﻿<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory">Chapter 10. EPL Reference: Functions</title>
	<link rel="stylesheet" href="css/espertech.css" type="text/css">
	<meta xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" name="generator" content="DocBook XSL-NS Stylesheets V1.74.0">
	<meta xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link rel="home" href="index.html" title="Esper Reference">
	<link rel="up" href="index.html" title="Esper Reference">
	<link rel="prev" href="epl-operator.html" title="Chapter 9. EPL Reference: Operators">
	<link rel="next" href="enumerationreference.html" title="Chapter 11. EPL Reference: Enumeration Methods">
</head>

<body>
	<p xmlns:d="http://docbook.org/ns/docbook" id="title"><a href="./index.html" class="site_href"><strong>www.espertech.com</strong></a><a href="http://www.espertech.com/esper/esper-documentation/" class="doc_href"><strong>Documentation</strong></a></p>
	<ul xmlns:d="http://docbook.org/ns/docbook" class="docnav">
		<li class="previous"><a accesskey="p" href="epl-operator.html"><strong>Prev</strong></a></li>
		<li class="next"><a accesskey="n" href="enumerationreference.html"><strong>Next</strong></a></li>
	</ul>
	<div class="chapter" lang="en-US">
		<div class="titlepage">
			<div>
				<div>
					<h2 class="title"><a id="functionreference"></a>Chapter 10. EPL Reference: Functions</h2>
				</div>
			</div>
		</div>
		<div class="toc">
			<dl>
				<dt><span class="sect1"><a href="functionreference.html#epl-single-row-function-ref">10.1. Single-Row Function Reference</a></span></dt>
				<dd>
					<dl>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-ref-case">10.1.1. The Case Control Flow Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-cast">10.1.2. The Cast Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-ref-coalesce">10.1.3. The Coalesce Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-ref-currentevaluationcontext">10.1.4. The Current_Evaluation_Context Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-ref-currenttime">10.1.5. The Current_Timestamp Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-eventidentityequals">10.1.6. The Event_Identity_Equals Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-exists">10.1.7. The Exists Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-grouping">10.1.8. The Grouping Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-groupingid">10.1.9. The Grouping_Id Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-instanceof">10.1.10. The Instance-Of Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-istream">10.1.11. The Istream Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-ref-minmax">10.1.12. The Min and Max Functions</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-ref-previous">10.1.13. The Previous Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-ref-previoustail">10.1.14. The Previous-Tail Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-ref-previouswindow">10.1.15. The Previous-Window Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-ref-previouscount">10.1.16. The Previous-Count Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-ref-prior">10.1.17. The Prior Function</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-single-row-function-typeof">10.1.18. The Type-Of Function</a></span></dt>
					</dl>
				</dd>
				<dt><span class="sect1"><a href="functionreference.html#epl-function-aggregation">10.2. Aggregation Functions</a></span></dt>
				<dd>
					<dl>
						<dt><span class="sect2"><a href="functionreference.html#epl-function-aggregation-std">10.2.1. SQL-Standard Functions</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-function-aggregation-event">10.2.2. Event Aggregation Functions</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-function-aggregation-approx">10.2.3. Approximation Aggregation Functions</a></span></dt>
						<dt><span class="sect2"><a href="functionreference.html#epl-function-aggregation-add">10.2.4. Additional Aggregation Functions</a></span></dt>
					</dl>
				</dd>
				<dt><span class="sect1"><a href="functionreference.html#epl-function-user-defined">10.3. User-Defined Functions</a></span></dt>
				<dt><span class="sect1"><a href="functionreference.html#functionreference-transpose">10.4. Select-Clause Transpose Function</a></span></dt>
				<dd>
					<dl>
						<dt><span class="sect2"><a href="functionreference.html#functionreference-transpose-insert-into">10.4.1. Transpose with Insert-Into</a></span></dt>
					</dl>
				</dd>
			</dl>
		</div>
		<div class="sect1" lang="en-US">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title"><a id="epl-single-row-function-ref"></a>10.1. Single-Row Function Reference</h2>
					</div>
				</div>
			</div>
			<p>
				Single-row functions return a single value for every single result row generated by your statement. These functions can appear anywhere where expressions are allowed.
			</p><a id="d0e27259" class="indexterm"></a><a id="d0e27262" class="indexterm"></a><a id="d0e27267" class="indexterm"></a>
			<p>
				EPL allows static Java library methods as single-row functions, and also features built-in single-row functions. In addition, EPL allows instance method invocations on named streams.
			</p>
			<p>
				You may also register your own single-row function name with the compiler so that your statements become less cluttered. This is described in detail in <a class="xref" href="extension.html#custom-singlerow-function" title="22.2. Single-Row Function">Section 22.2, “Single-Row Function”</a>. Single-row functions that return an object can be chained.
			</p>
			<p>
				The compiler auto-imports the following Java library packages:
			</p>
			<div class="itemizedlist">
				<ul compact="compact">
					<li>
						<p>
							java.lang.*
						</p>
					</li>
					<li>
						<p>
							java.math.*
						</p>
					</li>
					<li>
						<p>
							java.text.*
						</p>
					</li>
					<li>
						<p>
							java.util.*
						</p>
					</li>
				</ul>
			</div>
			<p>
				Thus Java static library methods can be used in all expressions as shown in below example:
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select symbol, Math.round(volume/1000)
from StockTickEvent#time(30 sec)</pre>
			<p>
				In general, arbitrary Java class names have to be fully qualified (e.g. java.lang.Math) but EPL provides a mechanism
				for user-controlled imports of classes and packages as outlined in <a class="xref" href="configuration.html#configuration-common-imports" title="17.4.2. Class and Package Imports">Section 17.4.2, “Class and Package Imports”</a>.
			</p>
			<p>
				The below table outlines the built-in single-row functions available.
			</p>
			<div class="table"><a id="epl-single-row-func"></a>
				<p class="title"><b>Table 10.1. Syntax and Results of Single-Row Functions</b></p>
				<div class="table-contents">
					<table summary="Syntax and Results of Single-Row Functions" border="1">
						<colgroup>
							<col>
							<col>
						</colgroup>
						<thead>
							<tr>
								<th>Single-row Function</th>
								<th>Result</th>
							</tr>
						</thead>
						<tbody>
							<tr>
								<td>
									<pre class="synopsis">case <span class="emphasis"><em>value</em></span> 
  when <span class="emphasis"><em>compare_value</em></span> then <span class="emphasis"><em>result</em></span>
  [when <span class="emphasis"><em>compare_value</em></span> then <span class="emphasis"><em>result</em></span> ...] 
  [else <span class="emphasis"><em>result</em></span>] 
  end </pre>
								</td>
								<td>
									<p>
										Returns <code class="literal">result</code> where the first <code class="literal">value</code> equals <code class="literal">compare_value</code>.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">case 
  when <span class="emphasis"><em>condition</em></span> then <span class="emphasis"><em>result</em></span>
  [when <span class="emphasis"><em>condition</em></span> then <span class="emphasis"><em>result</em></span> ...] 
  [else <span class="emphasis"><em>result</em></span>] 
  end</pre>
								</td>
								<td>
									<p>
										Returns the <code class="literal">result</code> for the first condition that is true.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">cast(<span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>type_name</em></span>)</pre>
								</td>
								<td>
									<p>
										Casts the result of an expression to the given type.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">coalesce(<span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>expression</em></span> ...])</pre>
								</td>
								<td>
									<p>
										Returns the first non-<code class="literal">null</code> value in the list, or <code class="literal">null</code> if there are no non-<code class="literal">null</code> values.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">current_evaluation_context()</pre>
								</td>
								<td>
									<p>
										Returns an object containing the runtime URI, statement name and context partition id (when applicable).
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">current_timestamp[()]</pre>
								</td>
								<td>
									<p>
										Returns the current runtime time as a <code class="literal">long</code> value. Reserved keyword with optional parenthesis.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">exists(<span class="emphasis"><em>dynamic_property_name</em></span>)</pre>
								</td>
								<td>
									<p>
										Returns true if the dynamic property exists for the event, or false if the property does not exist.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">instanceof(<span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>type_name</em></span> [, <span class="emphasis"><em>type_name</em></span> ...])</pre>
								</td>
								<td>
									<p>
										Returns true if the expression returns an object whose type is one of the types listed.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">istream()</pre>
								</td>
								<td>
									<p>
										Returns true if the event is part of the insert stream and false if the event is part of the remove stream.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">max(<span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>expression</em></span> ...])</pre>
								</td>
								<td>
									<p>
										Returns the highest numeric value among the 2 or more comma-separated expressions.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">min(<span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>expression</em></span> ...])</pre>
								</td>
								<td>
									<p>
										Returns the lowest numeric value among the 2 or more comma-separated expressions.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">prev(<span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>event_property)</em></span></pre>
								</td>
								<td>
									<p>
										Returns a property value or all properties of a previous event, relative to the event order within a data window, or according to an optional index parameter (N) the positional Nth-from-last value.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">prevtail(<span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>event_property)</em></span></pre>
								</td>
								<td>
									<p>
										Returns a property value or all properties of the first event in a data window relative to the event order within a data window, or according to an optional index parameter (N) the positional Nth-from-first value.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">prevwindow(<span class="emphasis"><em>event_property</em></span>)</pre>
								</td>
								<td>
									<p>
										Returns a single property value of all events or all properties of all events in a data window in the order that reflects the sort order of the data window.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">prevcount(<span class="emphasis"><em>event_property</em></span>)</pre>
								</td>
								<td>
									<p>
										Returns the count of events (number of data points) in a data window.
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">prior(<span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>event_property)</em></span></pre>
								</td>
								<td>
									<p>
										Returns a property value of a prior event, relative to the natural order of arrival of events
									</p>
								</td>
							</tr>
							<tr>
								<td>
									<pre class="synopsis">typeof(<span class="emphasis"><em>expression</em></span>)</pre>
								</td>
								<td>
									<p>
										If expression is a stream name, returns the event type name of the evaluated event, often used with variant streams.
										If expression is a property name or expression, returns the name of the expression result type.
									</p>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
			</div><br class="table-break">
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-ref-case"></a>10.1.1. The <code class="literal">Case</code> Control Flow Function</h3>
						</div>
					</div>
				</div><a id="d0e27599" class="indexterm"></a><a id="d0e27602" class="indexterm"></a>
				<p>
					The <code class="literal">case</code> control flow function has two versions. The first version takes a value and a list of compare values to compare against, and returns the result where the first value equals the compare value. The second version takes a list of conditions and returns the result for the first condition that is true.
				</p>
				<p>
					The return type of a <code class="literal">case</code> expression is the compatible aggregated type of all return values.
				</p>
				<p>
					The <code class="literal">case</code> expression is sometimes used with the <code class="literal">new</code> operator to return multiple results, see <a class="xref" href="epl-operator.html#epl-operator-new" title="9.14. The 'New' Keyword">Section 9.14, “The 'New' Keyword”</a>.
				</p>
				<p>
					The example below shows the first version of a <code class="literal">case</code> statement. It has a <code class="literal">String</code> return type and returns the value 'one'.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select case myexpression when 1 then 'one' when 2 then 'two' else 'more' end from ...</pre>
				<p>
					The second version of the <code class="literal">case</code> function takes a list of conditions. The next example has a <code class="literal">Boolean</code> return type and returns the boolean value true.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select case when 1&gt;0 then true else false end from ...</pre>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-cast"></a>10.1.2. The <code class="literal">Cast</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e27653" class="indexterm"></a><a id="d0e27656" class="indexterm"></a>
				<p>
					The <code class="literal">cast</code> function casts the return type of an expression to a designated type. The function accepts two parameters:
					The first parameter is the property name or expression that returns the value to be casted. The second parameter is the type to cast to.
					You can use the <code class="literal">as</code> keyword instead of comma (<code class="literal">,</code>) to separate parameters.
				</p>
				<p>
					Valid parameters for the second (type) parameter are:
				</p>
				<div class="itemizedlist">
					<ul>
						<li>
							<p>
								Any of the Java built-in types: <code class="literal">int, long, byte, short, char, double, float, string, BigInteger, BigDecimal</code>, where <code class="literal">string</code> is a short notation for <code class="literal">java.lang.String</code> and <code class="literal">BigInteger</code> as well as <code class="literal">BigDecimal</code> are the classes in <code class="literal">java.math</code>. The type name is not case-sensitive. Use <code class="literal">[]</code> for array types. For example:
							</p>
							<pre class="synopsis">cast(price, double)</pre>
							<p>
							</p>
						</li>
						<li>
							<p>
								The fully-qualified class name of the class to cast to, for example: </p>
							<pre class="synopsis">cast(product as org.myproducer.Product)</pre>
							<p>
							</p>
						</li>
						<li>
							<p>
								For parsing date-time values, any of the date-time types: <code class="literal">date, calendar, long, localdatetime, zoneddatetime, localdate, localtime</code>. For these types the <code class="literal">dateformat</code> parameter is required as discussed below.
							</p>
						</li>
					</ul>
				</div>
				<p>
					The <code class="literal">cast</code> function is often used to provide a type for dynamic (unchecked) properties. Dynamic properties are properties whose type is not known at compile type.
					These properties are always of type <code class="literal">java.lang.Object</code>.
				</p>
				<p>
					The <code class="literal">cast</code> function as shown in the next statement casts the dynamic "price" property of an "item" in the OrderEvent to a double value.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select cast(item.price?, double) from OrderEvent</pre>
				<p>
					The <code class="literal">cast</code> function returns a <code class="literal">null</code> value if the expression result cannot be casted to the desired type, or if the expression result itself is <code class="literal">null</code>.
				</p>
				<p>
					The <code class="literal">cast</code> function adheres to the following type conversion rules:
				</p>
				<div class="itemizedlist">
					<ul>
						<li>
							<p>
								For all numeric types, the <code class="literal">cast</code> function utilitzes <code class="literal">java.lang.Number</code> to convert numeric types, if required.
							</p>
						</li>
						<li>
							<p>
								For casts to <code class="literal">string</code> or <code class="literal">java.lang.String</code>, the function calls <code class="literal">toString</code> on the expression result.
							</p>
						</li>
						<li>
							<p>
								For casts to other objects including application objects, the <code class="literal">cast</code> function considers a Java class's superclasses as well as all directly or indirectly-implemented interfaces by superclasses.
							</p>
						</li>
					</ul>
				</div>
				<p>
					The <code class="literal">cast</code> function supports specifying a parameterized type, for example:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select cast(item.listOfAlternativeNames, java.util.List&lt;String&gt;) from OrderEvent</pre>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-single-row-function-cast-parsedate"></a>10.1.2.1. The <code class="literal">Cast</code> Function For Parsing Dates</h4>
							</div>
						</div>
					</div><a id="d0e27789" class="indexterm"></a>
					<p>
						The <code class="literal">cast</code> function can parse string-type date-time values to long-type milliseconds, <code class="literal">Date</code>, <code class="literal">Calendar</code>, <code class="literal">LocalDateTime</code>, <code class="literal">ZonedDateTime</code>, <code class="literal">LocalDate</code> and <code class="literal">LocalTime</code> objects.
					</p>
					<p>
						You must provide the <code class="literal">dateformat</code> named parameter as the last parameter to the <code class="literal">cast</code> function.
						The <code class="literal">dateformat</code> parameter expression must return a <code class="literal">String</code>-typed value, a <code class="literal">SimpleDateFormat</code>-type value or a <code class="literal">DateTimeFormatter</code>-type value.
						Return a <code class="literal">SimpleDateFormat</code> for <code class="literal">long</code>/<code class="literal">Date</code>/<code class="literal">Calendar</code>.
						Return a <code class="literal">DateTimeFormatter</code> for <code class="literal">LocalDateTime</code>/<code class="literal">ZonedDateTime</code>/<code class="literal">LocalDate</code>/<code class="literal">LocalTime</code>.
					</p>
					<p>
						The next EPL outputs the date May 2, 2010 as a <code class="literal">Date</code>-type value:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select cast('20100502', date, dateformat: 'yyyyMMdd') from OrderEvent</pre>
					<p>
						You may use date-time methods when <code class="literal">cast</code> is returning a date-time value. Expressions can be any expression and do not need to be string constants.
					</p>
					<p>
						You may parse dates that are ISO 8601-formatted dates by specifying <code class="literal">iso</code> as the date format. The ISO 8601 date format is described in <a class="xref" href="event_patterns.html#pattern-timer-schedule-date" title="7.6.5.1.1. Specifying Dates">Section 7.6.5.1.1, “Specifying Dates”</a>.
					</p>
					<p>
						For example, assuming the <code class="literal">orderDate</code> property is a ISO 8601 formatted date, the runtime can convert it to a long millisecond value like this:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select cast(orderDate, long, dateformat: 'iso') from OrderEvent</pre>
					<p>
						The next table shows the recognized date types available:
					</p>
					<div class="table"><a id="epl-single-row-function-cast-parsedate-types"></a>
						<p class="title"><b>Table 10.2. Date Types for Casting/Parsing</b></p>
						<div class="table-contents">
							<table summary="Date Types for Casting/Parsing" border="1">
								<colgroup>
									<col>
									<col>
								</colgroup>
								<thead>
									<tr>
										<th>Value provided to Cast as the second parameter</th>
										<th>Result Type</th>
									</tr>
								</thead>
								<tbody>
									<tr>
										<td><code class="literal">date</code>, <code class="literal">java.util.Date</code></td>
										<td><code class="literal">java.util.Date</code></td>
									</tr>
									<tr>
										<td><code class="literal">calendar</code>, <code class="literal">java.util.Calendar</code></td>
										<td><code class="literal">java.util.Calendar</code></td>
									</tr>
									<tr>
										<td><code class="literal">long</code>, <code class="literal">java.lang.Long</code></td>
										<td><code class="literal">long</code></td>
									</tr>
									<tr>
										<td><code class="literal">localdatetime</code>, <code class="literal">java.time.LocalDateTime</code></td>
										<td><code class="literal">java.time.LocalDateTime</code></td>
									</tr>
									<tr>
										<td><code class="literal">localdate</code>, <code class="literal">java.time.LocalDate</code></td>
										<td><code class="literal">java.time.LocalDate</code></td>
									</tr>
									<tr>
										<td><code class="literal">localtime</code>, <code class="literal">java.time.LocalTime</code></td>
										<td><code class="literal">java.time.LocalTime</code></td>
									</tr>
									<tr>
										<td><code class="literal">zoneddatetime</code>, <code class="literal">java.time.ZonedDateTime</code></td>
										<td><code class="literal">java.time.ZonedDateTime</code></td>
									</tr>
								</tbody>
							</table>
						</div>
					</div><br class="table-break">
				</div>
				<p>
					Additional examples are:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select cast(orderDate, localdatetime, dateformat:java.time.format.DateTimeFormatter.ISO_DATE_TIME) from OrderEvent</pre>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select cast(orderDate, calendar, dateformat:SimpleDateFormat.getInstance()) from OrderEvent</pre>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-single-row-function-cast-array"></a>10.1.2.2. Casting Arrays</h4>
							</div>
						</div>
					</div><a id="d0e27984" class="indexterm"></a>
					<p>
						You may add array brackets (<code class="literal">[]</code>) to the type to indicate that the cast-to type is an array. You may add array brackets with the <code class="literal">primitive</code> keyword (<code class="literal">[primitive]</code>) for casting to an array of primitive values.
					</p>
					<p>
						Some examples are:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select cast(string_array, string[]), cast(int_primitive_array, int[primitive]), 
  cast(int_boxed_array, int[]), cast(two_dimensional, string[][]), cast(object_array, java.lang.Object[]) from MyEvent</pre>
				</div>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-ref-coalesce"></a>10.1.3. The <code class="literal">Coalesce</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28010" class="indexterm"></a><a id="d0e28013" class="indexterm"></a>
				<p>
					The result of the <code class="literal">coalesce</code> function is the first expression in a list of expressions that returns a non-null value. The return type is the compatible aggregated type of all return values.
				</p>
				<p>
					This example returns a String-typed result of value 'foo':
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select coalesce(null, 'foo') from ...</pre>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-ref-currentevaluationcontext"></a>10.1.4. The <code class="literal">Current_Evaluation_Context</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28033" class="indexterm"></a><a id="d0e28036" class="indexterm"></a>
				<p>
					The <code class="literal">current_evaluation_context</code> function takes no parameters and returns expression evaluation contextual information as an object of type
					<code class="literal">com.espertech.esper.common.client.hook.expr.EPLExpressionEvaluationContext</code>. The object provides the runtime URI, the statement name and the context partition id of
					the currently-evaluated expression.
				</p>
				<p>
					For example:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select current_evaluation_context().getRuntimeURI() as runtimeURI from MyEvent</pre>
				<p>
					The context partition id will be <code class="literal">-1</code> when the statement is not associated to a context.
				</p>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-ref-currenttime"></a>10.1.5. The <code class="literal">Current_Timestamp</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28064" class="indexterm"></a><a id="d0e28067" class="indexterm"></a>
				<p>
					The <code class="literal">current_timestamp</code> function is a reserved keyword and requires no parameters. The result of the <code class="literal">current_timestamp</code> function is the <code class="literal">long</code>-type value of the current runtime time.
				</p>
				<p>
					The function returns the current runtime timestamp at the time of expression evaluation. When using external-timer events, the function provides the last value of the externally-supplied time at the time of expression evaluation.
				</p>
				<p>
					This example selects the current runtime time:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select current_timestamp from MyEvent
// equivalent to
select current_timestamp() from MyEvent</pre>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-eventidentityequals"></a>10.1.6. The <code class="literal">Event_Identity_Equals</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28095" class="indexterm"></a><a id="d0e28098" class="indexterm"></a>
				<p>
					The <code class="literal">event_identity_equals</code> function returns a boolean value indicating whether two events are the same event. The function accepts two parameters and both parameters must be a stream name.
					It validates that the event type of each stream is the same event type.
				</p>
				<p>
					In a subquery, the function can be used to exclude the currently-arriving event from results, as shown below.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select * from OrderEvent as arrivingEvent 
  where exists (select * from OrderEvent#time(5) as last5 
        where not event_identity_equals(arrivingEvent, last5) and arrivingEvent.orderId = last5.orderId)</pre>
				<p>
					You may also use this function with enumeration methods that process events. The next sample totals up window amounts excluding the current event.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select orderId, window(*)
  .aggregate(0d, (result, e) =&gt; result + (case when event_identity_equals(oe, e) then 0d else e.amount end)) as c0 
from OrderEvent#time(10) as oe</pre>
				<p>
					The function returns <code class="literal">null</code> when either of the streams have no event.
				</p>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-exists"></a>10.1.7. The <code class="literal">Exists</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28127" class="indexterm"></a><a id="d0e28130" class="indexterm"></a>
				<p>
					The <code class="literal">exists</code> function returns a boolean value indicating whether the dynamic property, provided as a parameter to the function, exists on the event. The <code class="literal">exists</code> function accepts a single dynamic property name as its only parameter.
				</p>
				<p>
					The <code class="literal">exists</code> function is for use with dynamic (unchecked) properties. Dynamic properties are properties whose type is not known at compile type. Dynamic properties return a null value
					if the dynamic property does not exists on an event, or if the dynamic property exists but the value of the dynamic property is null.
				</p>
				<p>
					The <code class="literal">exists</code> function as shown next returns true if the "item" property contains an object that has a "serviceName" property. It returns false if the "item" property is null, or if the "item" property does not
					contain an object that has a property named "serviceName" :
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select exists(item.serviceName?) from OrderEvent</pre>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-grouping"></a>10.1.8. The <code class="literal">Grouping</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28161" class="indexterm"></a><a id="d0e28164" class="indexterm"></a>
				<p>
					The <code class="literal">grouping</code> function is a SQL-standard function useful in statements that have a <code class="literal">group by</code>-clause and that utilize one of the <code class="literal">rollup</code>, <code class="literal">cube</code> or <code class="literal">grouping sets</code> keywords. The function can be used only in the <code class="literal">select</code>-clause, <code class="literal">having</code>-clause and <code class="literal">order by</code>-clauses.
				</p>
				<p>
					The function takes a single expression as a parameter and returns an integer value of zero or one indicating whether a specified expression in a <code class="literal">group-by</code>-clause is aggregated or not.
					The function returns 1 for aggregated or 0 for not aggregated.
				</p>
				<p>
					The <code class="literal">grouping</code> function can help you distinguish null values returned because of the output row's aggregation level from null values returned by event properties or other expressions.
				</p>
				<p>
					The parameter expression must match exactly one of the expressions in the <code class="literal">group-by</code>-clause.
				</p>
				<p>
					Please see an example in the next section.
				</p>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-groupingid"></a>10.1.9. The <code class="literal">Grouping_Id</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28218" class="indexterm"></a><a id="d0e28221" class="indexterm"></a>
				<p>
					The <code class="literal">grouping_id</code> function is a SQL-standard function useful in statements that have a <code class="literal">group by</code>-clause and that utilize one of the <code class="literal">rollup</code>, <code class="literal">cube</code> or <code class="literal">grouping sets</code> keywords. The function can be used only in the <code class="literal">select</code>-clause, <code class="literal">having</code>-clause and <code class="literal">order by</code>-clauses.
				</p>
				<p>
					The function takes one or more expressions as a parameter and returns an integer value indicating grouping level. The runtime computes the grouping level by taking the results of multiple <code class="literal">grouping</code> functions and concatenating them into a bit vector (a string of ones and zeros).
				</p>
				<p>
					Assume a car event that has a property for name, place and number of cars:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">create schema CarEvent(name string, place string, numcars int)</pre>
				<p>
					The next EPL computes the total number of cars for each of the following groupings: per name and place, per name, per place and overall.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select name, place, sum(numcars), grouping(name), grouping(place), grouping_id(name, place)
from CarEvent group by grouping sets((name, place),name, place,())</pre>
				<p>
					Assume your application processes a car event with properties like so: <code class="literal">CarEvent={name='skoda', place='france', numcars=100}</code>.
				</p>
				<p>
					The runtime outputs 4 rows as shown in the next table:
				</p>
				<div class="table"><a id="epl-single-row-function-grouping-out1"></a>
					<p class="title"><b>Table 10.3. Example Output for <code class="literal">Grouping</code> and <code class="literal">Grouping_id</code> Functions (CarEvent 1)</b></p>
					<div class="table-contents">
						<table summary="Example Output for Grouping and Grouping_id Functions (CarEvent 1)" border="1">
							<colgroup>
								<col>
								<col>
								<col>
								<col>
								<col>
								<col>
							</colgroup>
							<thead>
								<tr>
									<th>name</th>
									<th>place</th>
									<th>sum(numcars)</th>
									<th>grouping(name)</th>
									<th>grouping(place)</th>
									<th>grouping_id(name, place)</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td>skoda</td>
									<td>france</td>
									<td>100</td>
									<td>0</td>
									<td>0</td>
									<td>0</td>
								</tr>
								<tr>
									<td>skoda</td>
									<td>null</td>
									<td>100</td>
									<td>0</td>
									<td>1</td>
									<td>1</td>
								</tr>
								<tr>
									<td>null</td>
									<td>france</td>
									<td>100</td>
									<td>1</td>
									<td>0</td>
									<td>2</td>
								</tr>
								<tr>
									<td>null</td>
									<td>null</td>
									<td>100</td>
									<td>1</td>
									<td>1</td>
									<td>3</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div><br class="table-break">
				<p>
					Assume your application processes a second car event: <code class="literal">CarEvent={name='skoda', place='germany', numcars=75}</code>.
				</p>
				<p>
					The runtime outputs 4 rows as shown in the next table:
				</p>
				<div class="table"><a id="epl-single-row-function-grouping-out2"></a>
					<p class="title"><b>Table 10.4. Example Output for <code class="literal">Grouping</code> and <code class="literal">Grouping_id</code> Functions (CarEvent 2)</b></p>
					<div class="table-contents">
						<table summary="Example Output for Grouping and Grouping_id Functions (CarEvent 2)" border="1">
							<colgroup>
								<col>
								<col>
								<col>
								<col>
								<col>
								<col>
							</colgroup>
							<thead>
								<tr>
									<th>name</th>
									<th>place</th>
									<th>sum(numcars)</th>
									<th>grouping(name)</th>
									<th>grouping(place)</th>
									<th>grouping_id(name, place)</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td>skoda</td>
									<td>germany</td>
									<td>75</td>
									<td>0</td>
									<td>0</td>
									<td>0</td>
								</tr>
								<tr>
									<td>skoda</td>
									<td>null</td>
									<td>175</td>
									<td>0</td>
									<td>1</td>
									<td>1</td>
								</tr>
								<tr>
									<td>null</td>
									<td>germany</td>
									<td>75</td>
									<td>1</td>
									<td>0</td>
									<td>2</td>
								</tr>
								<tr>
									<td>null</td>
									<td>null</td>
									<td>175</td>
									<td>1</td>
									<td>1</td>
									<td>3</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div><br class="table-break">
				<p>
					The parameter expressions must match exactly to expressions in the <code class="literal">group-by</code>-clause.
				</p>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-instanceof"></a>10.1.10. The <code class="literal">Instance-Of</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28456" class="indexterm"></a><a id="d0e28459" class="indexterm"></a>
				<p>
					The <code class="literal">instanceof</code> function returns a boolean value indicating whether the type of value returned by the expression is one of the given types. The first parameter to the <code class="literal">instanceof</code> function is an expression to evaluate. The second and subsequent parameters are Java type names.
				</p>
				<p>
					The function determines the return type of the expression at runtime by evaluating the expression, and compares the type of object returned by the expression to the defined types.
					If the type of object returned by the expression matches any of the given types, the function returns <code class="literal">true</code>. If the expression returned <code class="literal">null</code> or a type that does not
					match any of the given types, the function returns <code class="literal">false</code>.
				</p>
				<p>
					The <code class="literal">instanceof</code> function is often used in conjunction with dynamic (unchecked) properties. Dynamic properties are properties whose type is not known at compile type.
				</p>
				<p>
					This example uses the <code class="literal">instanceof</code> function to select different properties based on the type:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select case when instanceof(item, com.mycompany.Service) then serviceName?
  when instanceof(item, com.mycompany.Product) then productName? end 
  from OrderEvent</pre>
				<p>
					The <code class="literal">instanceof</code> function returns <code class="literal">false</code> if the expression tested by instanceof returned null.
				</p>
				<p>
					Valid parameters for the type parameter list are:
				</p>
				<div class="itemizedlist">
					<ul>
						<li>
							<p>
								Any of the Java built-in types: <code class="literal">int, long, byte, short, char, double, float, string</code>, where <code class="literal">string</code> is a short notation for <code class="literal">java.lang.String</code>. The type name is not case-sensitive. For example, the next function tests if the dynamic "price" property is either of type float or type double:
							</p>
							<pre class="synopsis">instanceof(price?, double, float)</pre>
							<p>
							</p>
						</li>
						<li>
							<p>
								The fully-qualified class name of the class to cast to, for example: </p>
							<pre class="synopsis">instanceof(product, org.myproducer.Product)</pre>
							<p>
							</p>
						</li>
					</ul>
				</div>
				<p>
					The function considers an event class's superclasses as well as all the directly or indirectly-implemented interfaces by superclasses.
				</p>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-istream"></a>10.1.11. The <code class="literal">Istream</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28535" class="indexterm"></a><a id="d0e28538" class="indexterm"></a>
				<p>
					The <code class="literal">istream</code> function returns a boolean value indicating whether within the context of expression evaluation the current event or set of events (joins) are part of the insert stream (true) or part of the remove stream (false). The function takes no parameters.
				</p>
				<p>
					Use the <code class="literal">istream</code> function with data windows and <code class="literal">select irstream</code> and <code class="literal">insert irstream into</code>.
				</p>
				<p>
					In the following example the <code class="literal">istream</code> function always returns boolean true since no data window is declared:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select irstream *, istream() from OrderEvent</pre>
				<p>
					The next example declares a data window. For newly arriving events the function returns boolean true, for events that expire after 10 seconds the function returns boolean false:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select irstream *, istream() from OrderEvent#time(10 sec)</pre>
				<p>
					The <code class="literal">istream</code> function returns true for all cases where insert or remove stream does not apply, such as when used in parameter expressions to data windows
					or in stream filter expressions.
				</p>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-ref-minmax"></a>10.1.12. The <code class="literal">Min</code> and <code class="literal">Max</code> Functions</h3>
						</div>
					</div>
				</div><a id="d0e28584" class="indexterm"></a><a id="d0e28587" class="indexterm"></a><a id="d0e28592" class="indexterm"></a><a id="d0e28595" class="indexterm"></a>
				<p>
					The <code class="literal">min</code> and <code class="literal">max</code> function take two or more parameters that itself can be expressions. The <code class="literal">min</code> function returns the lowest numeric value among the 2 or more comma-separated expressions, while the <code class="literal">max</code> function returns the highest numeric value.
					The return type is the compatible aggregated type of all return values.
				</p>
				<p>
					The next example shows the <code class="literal">max</code> function that has a <code class="literal">Double</code> return type and returns the value 1.1.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select max(1, 1.1, 2 * 0.5) from ...</pre>
				<p>
					The <code class="literal">min</code> function returns the lowest value. The statement below uses the function to determine the smaller of two timestamp values.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select symbol, min(ticks.timestamp, news.timestamp) as minT
	from StockTickEvent#time(30 sec) as ticks, NewsEvent#time(30 sec) as news
	where ticks.symbol = news.symbol</pre>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-ref-previous"></a>10.1.13. The <code class="literal">Previous</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28637" class="indexterm"></a><a id="d0e28640" class="indexterm"></a>
				<p>
					The <code class="literal">prev</code> function returns the property value or all event properties of a previous event. For data windows that introduce a sort order other than the order of arrival, such as the sorted data window and the time order data window, the function returns the event at the specified position.
				</p>
				<p>
					The <code class="literal">prev</code> function is not an aggregation function and therefore does not return results per group when used with <code class="literal">group by</code>. Please consider the <code class="literal">last</code>, <code class="literal">lastever</code> or <code class="literal">first</code> aggregation functions instead as described in <a class="xref" href="functionreference.html#epl-function-aggregation-event" title="10.2.2. Event Aggregation Functions">Section 10.2.2, “Event Aggregation Functions”</a>. You must use an aggregation function instead of <code class="literal">prev</code> when querying a named window or table.
				</p>
				<p>
					The first parameter to the <code class="literal">prev</code> function is an index parameter and denotes the i-th previous event, in the order established by the data window. If no index is provided, the default index is 1 and the function returns the previous event.
					The second parameter is a property name or stream name. If specifying a property name, the function returns the value for the previous event property value. If specifying a stream name, the function returns the previous event underlying object.
				</p>
				<p>
					This example selects the value of the <code class="literal">price</code> property of the 2nd-previous event from the current Trade event:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prev(2, price) from Trade#length(10)</pre>
				<p>
					By using the stream alias in the <code class="literal">previous</code> function, the next example selects the trade event itself that is immediately previous to the current Trade event
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prev(1, trade) from Trade#length(10) as trade</pre>
				<p>
					Since the <code class="literal">prev</code> function takes the order established by the data window into account, the function works well with sorted windows.
				</p>
				<p>
					In the following example the statement selects the symbol of the 3 Trade events that had the largest, second-largest and third-largest volume.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prev(0, symbol), prev(1, symbol), prev(2, symbol)
  from Trade#sort(3, volume desc)</pre>
				<p>
					The i-th previous event parameter can also be an expression returning an Integer-type value. The next statement joins the Trade data window with an <code class="literal">RankSelectionEvent</code> event that provides a <code class="literal">rank</code> property used to look up a certain position in the sorted Trade data window:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prev(rank, symbol) from Trade#sort(10, volume desc), RankSelectionEvent unidirectional</pre>
				<p>
					Use the <code class="literal">prev</code> function in combination with a grouped data window to access a previous event per grouping criteria.
				</p>
				<p>
					The example below returns the price of the previous Trade event for the same symbol, or <code class="literal">null</code> if for that symbol there is no previous Trade event:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prev(1, price) from Trade#groupwin(symbol)#length(2)</pre>
				<p>
					The <code class="literal">prev</code> function returns a <code class="literal">null</code> value if the data window does not currently hold the i-th previous event. The example below illustrates this using a time batch window. Here the <code class="literal">prev</code>
					function returns a null value for any events in which the previous event is not in the same batch of events. Note that the <code class="literal">prior</code> function as discussed below can be used if a null value is not the desired result.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prev(1, symbol) from Trade#time_batch(1 min)</pre>
				<p>
					An alternative form of the <code class="literal">prev</code> function allows the index to not appear or appear after the property name if the index value is a constant and not an expression:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prev(1, symbol) from Trade
// ... equivalent to ...
select prev(symbol) from Trade
// ... and ...
select prev(symbol, 1) from Trade</pre>
				<p>
					The combination of the <code class="literal">prev</code> function and <code class="literal">#groupwin</code> returns the property value for a previous event in the given data window group.
				</p>
				<p>
					The following example returns for each event the current smallest price per symbol:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select symbol, prev(0, price) as topPricePerSymbol 
from Trade#groupwin(symbol)#sort(1, price asc)</pre>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-single-row-function-ref-prev-restrictions"></a>10.1.13.1. Restrictions</h4>
							</div>
						</div>
					</div>
					<p>
						The following restrictions apply to the <code class="literal">prev</code> functions and its results:
					</p>
					<div class="itemizedlist">
						<ul compact="compact">
							<li>
								<p>
									The function always returns a <code class="literal">null</code> value for remove stream (old data) events.
								</p>
							</li>
							<li>
								<p>
									The function requires a data window, or <code class="literal">#groupwin</code> and a data window. See <a class="xref" href="epl-views.html#win-views" title="14.3. Data Windows">Section 14.3, “Data Windows”</a> for data windows.
								</p>
							</li>
						</ul>
					</div>
				</div>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-single-row-function-ref-prev-prior"></a>10.1.13.2. Comparison to the <code class="literal">Prior</code> Function</h4>
							</div>
						</div>
					</div>
					<p>
						The <code class="literal">prev</code> function is similar to the <code class="literal">prior</code> function. The key differences between the two functions are as follows:
					</p>
					<div class="itemizedlist">
						<ul compact="compact">
							<li>
								<p>
									The <code class="literal">prev</code> function returns previous events in the order provided by the data window, while the <code class="literal">prior</code> function returns prior events in the order of arrival as posted by a stream's declared windows.
								</p>
							</li>
							<li>
								<p>
									The <code class="literal">prev</code> function requires a data window while the <code class="literal">prior</code> function does not have any such requirement.
								</p>
							</li>
							<li>
								<p>
									The <code class="literal">prev</code> function returns the previous event grouped by a criteria by combining the <code class="literal">#groupwin</code> and a data window. The <code class="literal">prior</code> function returns prior events posted regardless of data window grouping.
								</p>
							</li>
							<li>
								<p>
									The <code class="literal">prev</code> function returns a <code class="literal">null</code> value for remove stream events, i.e. for events leaving a data window.
									The <code class="literal">prior</code> function does not have this restriction.
								</p>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-ref-previoustail"></a>10.1.14. The <code class="literal">Previous-Tail</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28843" class="indexterm"></a><a id="d0e28846" class="indexterm"></a>
				<p>
					The <code class="literal">prevtail</code> function returns the property value or all event properties of the positional-first event in a data window. For data windows that introduce a sort order other than the order of arrival, such as the sorted data window and the time order data window, the function returns the first event at the specified position.
				</p>
				<p>
					The <code class="literal">prevtail</code> function is not an aggregation function and therefore does not return results per group when used with <code class="literal">group by</code>. Please consider the <code class="literal">first</code>, <code class="literal">firstever</code> or <code class="literal">window</code> aggregation functions instead as described in <a class="xref" href="functionreference.html#epl-function-aggregation-event" title="10.2.2. Event Aggregation Functions">Section 10.2.2, “Event Aggregation Functions”</a>. You must use an aggregation function instead of <code class="literal">prevtail</code> when querying a named window or table.
				</p>
				<p>
					The first parameter is an index parameter and denotes the i-th from-first event in the order established by the data window. If no index is provided the default is zero and the function returns the first event in the data window.
					The second parameter is a property name or stream name. If specifying a property name, the function returns the value for the previous event property value. If specifying a stream name, the function returns the previous event underlying object.
				</p>
				<p>
					This example selects the value of the <code class="literal">price</code> property of the first (oldest) event held in the length window:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevtail(price) from Trade#length(10)</pre>
				<p>
					By using the stream alias in the <code class="literal">prevtail</code> function, the next example selects the trade event itself that is the second event held in the length window:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevtail(1, trade) from Trade#length(10) as trade</pre>
				<p>
					Since the <code class="literal">prevtail</code> function takes the order established by the data window into account, the function works well with sorted windows.
				</p>
				<p>
					In the following example the statement selects the symbol of the 3 Trade events that had the smallest, second-smallest and third-smallest volume.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevtail(0, symbol), prevtail(1, symbol), prevtail(2, symbol)
  from Trade#sort(3, volume asc)</pre>
				<p>
					The i-th previous event parameter can also be an expression returning an Integer-type value. The next statement joins the Trade data window with an <code class="literal">RankSelectionEvent</code> event that provides a <code class="literal">rank</code> property used to look up a certain position in the sorted Trade data window:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevtail(rank, symbol) from Trade#sort(10, volume asc), RankSelectionEvent unidirectional</pre>
				<p>
					The <code class="literal">prev</code> function returns a <code class="literal">null</code> value if the data window does not currently holds positional-first or the Nth-from-first event. For batch data windows the value returned is relative to the current batch.
				</p>
				<p>
					The following example returns the first and second symbol value in the batch:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevtail(0, symbol), prevtail(1, symbol) from Trade#time_batch(1 min)</pre>
				<p>
					An alternative form of the <code class="literal">prevtail</code> function allows the index to not appear or appear after the property name if the index value is a constant and not an expression:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevtail(1, symbol) from Trade
// ... equivalent to ...
select prevtail(symbol) from Trade
// ... and ...
select prevtail(symbol, 1) from Trade</pre>
				<p>
					The combination of the <code class="literal">prevtail</code> function and <code class="literal">#groupwin</code> returns the property value for a positional first event in the given data window group.
				</p>
				<p>
					Let's look at an example. This statement outputs the oldest price per symbol retaining the last 10 prices per symbol:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select symbol, prevtail(0, price) as oldestPrice
from Trade#groupwin(symbol)#length(10)</pre>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-single-row-function-ref-prevtail-restrictions"></a>10.1.14.1. Restrictions</h4>
							</div>
						</div>
					</div>
					<p>
						The following restrictions apply to the <code class="literal">prev</code> functions and its results:
					</p>
					<div class="itemizedlist">
						<ul compact="compact">
							<li>
								<p>
									The function always returns a <code class="literal">null</code> value for remove stream (old data) events.
								</p>
							</li>
							<li>
								<p>
									The function requires a data window, or a <code class="literal">#groupwin</code> and a data window. See <a class="xref" href="epl-views.html#win-views" title="14.3. Data Windows">Section 14.3, “Data Windows”</a> for built-in data windows.
								</p>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-ref-previouswindow"></a>10.1.15. The <code class="literal">Previous-Window</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e28973" class="indexterm"></a><a id="d0e28976" class="indexterm"></a>
				<p>
					The <code class="literal">prevwindow</code> function returns property values or all event properties for all events in a data window. For data windows that introduce a sort order other than the order of arrival, such as the sorted data window and the time order data window, the function returns the event data sorted in that order, otherwise it returns the events sorted by order of arrival with the newest arriving event first.
				</p>
				<p>
					The <code class="literal">prevwindow</code> function is not an aggregation function and therefore does not return results per group when used with <code class="literal">group by</code>. Please consider the <code class="literal">window</code> aggregation function instead as described in <a class="xref" href="functionreference.html#epl-function-aggregation-event" title="10.2.2. Event Aggregation Functions">Section 10.2.2, “Event Aggregation Functions”</a>. You must use an aggregation function instead of <code class="literal">prevwindow</code> when querying a named window or table.
				</p>
				<p>
					The single parameter is a property name or stream name. If specifying a property name, the function returns the value of the event property for all events held by the data window. If specifying a stream name, the function returns the event underlying object for all events held by the data window.
				</p>
				<p>
					This example selects the value of the <code class="literal">price</code> property of all events held in the length window:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevwindow(price) from Trade#length(10)</pre>
				<p>
					By using the stream alias in the <code class="literal">prevwindow</code> function, the next example selects all trade events held in the length window:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevwindow(trade) from Trade#length(10) as trade</pre>
				<p>
					When used with a data window that introduces a certain sort order, the <code class="literal">prevwindow</code> function returns events sorted according to that sort order.
				</p>
				<p>
					The next statement outputs for every arriving event the current 10 underying trade event objects that have the largest volume:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevwindow(trade) from Trade#sort(10, volume desc) as trade</pre>
				<p>
					The <code class="literal">prevwindow</code> function returns a <code class="literal">null</code> value if the data window does not currently hold any events.
				</p>
				<p>
					The combination of the <code class="literal">prevwindow</code> function and <code class="literal">#groupwin</code> returns the property value(s) for all events in the given data window group.
				</p>
				<p>
					This example statement outputs all prices per symbol retaining the last 10 prices per symbol:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select symbol, prevwindow(price) from Trade#groupwin(symbol)#length(10)</pre>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-single-row-function-ref-prevwindow-restrictions"></a>10.1.15.1. Restrictions</h4>
							</div>
						</div>
					</div>
					<p>
						The following restrictions apply to the <code class="literal">prev</code> functions and its results:
					</p>
					<div class="itemizedlist">
						<ul compact="compact">
							<li>
								<p>
									The function always returns a <code class="literal">null</code> value for remove stream (old data) events.
								</p>
							</li>
							<li>
								<p>
									The function requires a data window, or <code class="literal">#groupwin</code> and a data window. See <a class="xref" href="epl-views.html#win-views" title="14.3. Data Windows">Section 14.3, “Data Windows”</a> for built-in data windows.
								</p>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-ref-previouscount"></a>10.1.16. The <code class="literal">Previous-Count</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e29076" class="indexterm"></a><a id="d0e29079" class="indexterm"></a>
				<p>
					The <code class="literal">prevcount</code> function returns the number of events held in a data window.
				</p>
				<p>
					The <code class="literal">prevcount</code> function is not an aggregation function and therefore does not return results per group when used with <code class="literal">group by</code>. Please consider the <code class="literal">count(*)</code> or <code class="literal">countever</code> aggregation functions instead as described in <a class="xref" href="functionreference.html#epl-function-aggregation" title="10.2. Aggregation Functions">Section 10.2, “Aggregation Functions”</a>. You must use an aggregation function instead of <code class="literal">prevcount</code> when querying a named window or table.
				</p>
				<p>
					The single parameter is a property name or stream name of the data window to return the count for.
				</p>
				<p>
					This example selects the number of data points for the <code class="literal">price</code> property held in the length window:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevcount(price) from Trade#length(10)</pre>
				<p>
					By using the stream alias in the <code class="literal">prevcount</code> function the next example selects the count of trade events held in the length window:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prevcount(trade) from Trade#length(10) as trade</pre>
				<p>
					The combination of the <code class="literal">prevcount</code> function and <code class="literal">#groupwin</code> returns the count of events in the given data window group.
				</p>
				<p>
					This example statement outputs the number of events retaining the last 10 events per symbol:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select symbol, prevcount(price) from Trade#groupwin(symbol)#length(10)</pre>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-single-row-function-ref-prevcount-restrictions"></a>10.1.16.1. Restrictions</h4>
							</div>
						</div>
					</div>
					<p>
						The following restrictions apply to the <code class="literal">prev</code> functions and its results:
					</p>
					<div class="itemizedlist">
						<ul compact="compact">
							<li>
								<p>
									The function always returns a <code class="literal">null</code> value for remove stream (old data) events.
								</p>
							</li>
							<li>
								<p>
									The function requires a data window, or a <code class="literal">#groupwin</code> and a data window. See <a class="xref" href="epl-views.html#win-views" title="14.3. Data Windows">Section 14.3, “Data Windows”</a> for built-in data windows.
								</p>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-ref-prior"></a>10.1.17. The <code class="literal">Prior</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e29165" class="indexterm"></a><a id="d0e29168" class="indexterm"></a>
				<p>
					The <code class="literal">prior</code> function returns the property value of a prior event.
					The first parameter is an expression returning a constant integer-typed value that denotes the i-th prior event in the natural order of arrival.
					The second parameter is a property name for which the function returns the value for the prior event.
					The second parameter is a property name or stream name. If specifying a property name, the function returns the property value for the prior event. If specifying a stream name, the function returns the prior event underlying object.
				</p>
				<p>
					This example selects the value of the <code class="literal">price</code> property of the 2nd-prior event to the current Trade event.
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prior(2, price) from Trade</pre>
				<p>
					By using the stream alias in the <code class="literal">prior</code> function, the next example selects the trade event itself that is immediately prior to the current Trade event
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select prior(1, trade) from Trade as trade</pre>
				<p>
					The <code class="literal">prior</code> function can be used on any event stream and does not require declaring a data window. The function operates on the order of arrival of events by the event stream that provides the events.
				</p>
				<p>
					The next statement uses a time batch window to compute an average volume for 1 minute of Trade events, posting results every minute. The select-clause
					employs the <code class="literal">prior</code> function to select the current average and the average before the current average:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select average, prior(1, average) 
    from TradeAverages#time_batch(1 min)#uni(volume)</pre>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-single-row-function-typeof"></a>10.1.18. The <code class="literal">Type-Of</code> Function</h3>
						</div>
					</div>
				</div><a id="d0e29210" class="indexterm"></a><a id="d0e29213" class="indexterm"></a>
				<p>
					The <code class="literal">typeof</code> function, when parameterized by a stream name, returns the event type name of the evaluated event which can be useful with variant streams. When parameterized by an expression or property name, the function returns the type name of the expression result or <code class="literal">null</code> if the expression result is null.
				</p>
				<p>
					In summary, the function determines the return type of the expression at runtime by evaluating the expression and returns the type name of the expression result.
				</p>
				<p>
					The <code class="literal">typeof</code> function is often used in conjunction with variant streams. A variant stream is a predefined stream into which events of multiple disparate event types can be inserted. The <code class="literal">typeof</code> function, when passed a stream name alias, returns the name of the event type of each event in the stream.
				</p>
				<p>
					The following example elaborates on the use of variant streams with <code class="literal">typeof</code>. The first statement declares a variant stream <code class="literal">SequencePatternStream</code>:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">create variant schema SequencePatternStream as *</pre>
				<p>
					The next statement inserts all order events and is followed by a statement to insert all product events:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">insert into SequencePatternStream select * from OrderEvent;</pre>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">insert into SequencePatternStream select * from PriceEvent;</pre>
				<p>
					This example statement returns the event type name for each event in the variant stream:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select typeof(sps) from SequencePatternStream as sps</pre>
				<p>
					The next example statement detects a pattern by utilizing the <code class="literal">typeof</code> function to find pairs of order event immediately followed by product event:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select * from SequencePatternStream match_recognize(
  measures A as a, B as b
  pattern (A B)
  define A as typeof(A) = "OrderEvent",
         B as typeof(B) = "ProductEvent"
  )</pre>
				<p>
					When passing a property name to the <code class="literal">typeof</code> function, the function evaluates whether the property type is event type (a fragment event type). If the property type is event type, the function returns the type name of the event in the property value or <code class="literal">null</code> if not provided.
					If the property type is not event type, the function returns the simple class name of the property value.
				</p>
				<p>
					When passing an expression to the <code class="literal">typeof</code> function, the function evaluates the expression and returns the simple class name of the expression result value or <code class="literal">null</code> if the expression result value is null.
				</p>
				<p>
					This example statement returns the simple class name of the value of the dynamic property <code class="literal">prop</code> of events in stream <code class="literal">MyStream</code>, or a <code class="literal">null</code> value if the property is not found for an event or the property value itself is <code class="literal">null</code>:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select typeof(prop?) from MyStream</pre>
				<p>
					When using subclasses or interface implementations as event classes or when using Map-event type inheritance, the function returns the event type name provided when the class or Map-type event was registered, or if the event type was not registered, the function returns the fully-qualified class name.
				</p>
			</div>
		</div>
		<div class="sect1" lang="en-US">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title"><a id="epl-function-aggregation"></a>10.2. Aggregation Functions</h2>
					</div>
				</div>
			</div><a id="d0e29300" class="indexterm"></a>
			<p>
				Aggregation functions are stateful and consider sets of events or value points. The <code class="literal">group by</code> clause is often used in conjunction with aggregation functions to group the result-set by one or more columns.
			</p>
			<p>
				Aggregation functions can be a column type for table declarations. This allows easy sharing of aggregated state, co-location of aggregations and other data
				as well as co-aggregation by multiple statements into the same aggregation state.
				Please see <a class="xref" href="nwtable.html#nwtable-overview-table" title="6.1.2. Table Overview">Section 6.1.2, “Table Overview”</a> for details.
			</p>
			<p>
				The EPL language extends the standard SQL aggregation functions by allowing filters and by further useful aggregation functions that can track a data window or compute event rates, for example. Your application may also add its own aggregation function as <a class="xref" href="extension.html#custom-aggregation-function" title="22.5. Aggregation Function">Section 22.5, “Aggregation Function”</a> describes.
			</p>
			<p>
				The EPL language allows each aggregation function to specify its own grouping criteria. Please find further information in <a class="xref" href="epl_clauses.html#epl-grouping-agglocal" title="5.6.4. Specifying Grouping for Each Aggregation Function">Section 5.6.4, “Specifying Grouping for Each Aggregation Function”</a>.
			</p>
			<p>
				The EPL language allows each aggregation function to specify its own filter criteria. Please find further information in <a class="xref" href="epl_clauses.html#epl-grouping-aggfilter" title="5.6.5. Specifying a Filter Expression for Each Aggregation Function">Section 5.6.5, “Specifying a Filter Expression for Each Aggregation Function”</a>.
			</p>
			<p>
				Aggregation values are always computed incrementally: Insert and remove streams result in aggregation value changes. The exceptions are fire-and-forget queries and joins when using the <code class="literal">unidirectional</code> keyword. Aggregation functions are optimized to retain the minimal
				information necessary to compute the aggregated result, and to share aggregation state between eligible other aggregation functions in the same statement so that same-kind aggregation state is
				never held multiple times unless required.
			</p>
			<p>
				Most aggregation functions can also be used with unbound streams when no data window is specified. A few aggregation functions require a data window or named window as documented below.
			</p>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-function-aggregation-std"></a>10.2.1. SQL-Standard Functions</h3>
						</div>
					</div>
				</div>
				<p>
					The SQL-standard aggregation functions are shown in below table.
				</p>
				<div class="table"><a id="epl-grouping-aggregate-func-table-SQL"></a>
					<p class="title"><b>Table 10.5. Syntax and Results of SQL-Standard Aggregation Functions</b></p>
					<div class="table-contents">
						<table summary="Syntax and Results of SQL-Standard Aggregation Functions" border="1">
							<colgroup>
								<col>
								<col>
							</colgroup>
							<thead>
								<tr>
									<th>Aggregate Function</th>
									<th>Result</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td>
										<pre class="synopsis">avedev([all|distinct] <span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>filter_expr</em></span>])</pre>
									</td>
									<td>
										<p>
											Mean deviation of the (distinct) values in the expression, returning a value of <code class="literal">double</code> type.
										</p>
										<p>
											The optional filter expression limits the values considered for computing the mean deviation.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<pre class="synopsis">avg([all|distinct] <span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>filter_expr</em></span>])</pre>
									</td>
									<td>
										<p>
											Average of the (distinct) values in the expression, returning a value of <code class="literal">double</code> type.
										</p>
										<p>
											The optional filter expression limits the values considered for computing the average.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<pre class="synopsis">count([all|distinct] <span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>filter_expr</em></span>])</pre>
									</td>
									<td>
										<p>
											Number of the (distinct) non-null values in the expression, returning a value of <code class="literal">long</code> type.
										</p>
										<p>
											The optional filter expression limits the values considered for the count.
										</p>
										<p>
											Further detail on key expressions that return arrays can be found at <a class="xref" href="epl_clauses.html#epl-syntax-compositekey" title="5.2.13. Composite Keys and Array Values as Keys">Section 5.2.13, “Composite Keys and Array Values as Keys”</a>.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<pre class="synopsis">count(* [, <span class="emphasis"><em>filter_expr</em></span>])</pre>
									</td>
									<td>
										<p>
											Number of events, returning a value of <code class="literal">long</code> type.
										</p>
										<p>
											The optional filter expression limits the values considered for the count.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<p>
										</p>
										<pre class="synopsis">max([all|distinct] <span class="emphasis"><em>expression</em></span>)</pre>
										<p>
										</p>
										<p>
										</p>
										<pre class="synopsis">fmax([all|distinct] <span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>filter_expr</em></span>)</pre>
										<p>
										</p>
									</td>
									<td>
										<p>
											Highest (distinct) value in the expression, returning a value of the same type as the expression itself returns.
										</p>
										<p>
											Use <code class="literal">fmax</code> to provide a filter expression that limits the values considered for computing the maximum.
										</p>
										<p>
											Consider using <code class="literal">maxby</code> instead if return values must include additional properties.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<p>
										</p>
										<pre class="synopsis">maxever([all|distinct] <span class="emphasis"><em>expression</em></span>)</pre>
										<p>
										</p>
										<p>
										</p>
										<pre class="synopsis">fmaxever([all|distinct] <span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>filter_expr</em></span>)</pre>
										<p>
										</p>
									</td>
									<td>
										<p>
											Highest (distinct) value - ever - in the expression, returning a value of the same type as the expression itself returns.
										</p>
										<p>
											Use <code class="literal">fmaxever</code> to provide a filter expression that limits the values considered for computing the maximum.
										</p>
										<p>
											Consider using <code class="literal">maxbyever</code> instead if return values must include additional properties.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<pre class="synopsis">median([all|distinct] <span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>filter_expr</em></span>])</pre>
									</td>
									<td>
										<p>
											Median (distinct) value in the expression, returning a value of <code class="literal">double</code> type. Double Not-a-Number (NaN) values are ignored in the median computation.
										</p>
										<p>
											The optional filter expression limits the values considered for computing the median.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<p>
										</p>
										<pre class="synopsis">min([all|distinct] <span class="emphasis"><em>expression</em></span>)</pre>
										<p>
										</p>
										<p>
										</p>
										<pre class="synopsis">fmin([all|distinct] <span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>filter_expr</em></span>)</pre>
										<p>
										</p>
									</td>
									<td>
										<p>
											Lowest (distinct) value in the expression, returning a value of the same type as the expression itself returns.
										</p>
										<p>
											Use <code class="literal">fmin</code> to provide a filter expression that limits the values considered for computing the maximum.
										</p>
										<p>
											Consider using <code class="literal">minby</code> instead if return values must include additional properties.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<p>
										</p>
										<pre class="synopsis">minever([all|distinct] <span class="emphasis"><em>expression</em></span>)</pre>
										<p>
										</p>
										<p>
										</p>
										<pre class="synopsis">fminever([all|distinct] <span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>filter_expr</em></span>)</pre>
										<p>
										</p>
									</td>
									<td>
										<p>
											Lowest (distinct) value - ever - in the expression, returning a value of the same type as the expression itself returns.
										</p>
										<p>
											Use <code class="literal">fminever</code> to provide a filter expression that limits the values considered for computing the maximum.
										</p>
										<p>
											Consider using <code class="literal">minbyever</code> instead if return values must include additional properties.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<pre class="synopsis">stddev([all|distinct] <span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>filter_expr</em></span>])</pre>
									</td>
									<td>
										<p>
											Standard deviation of the (distinct) values in the expression, returning a value of <code class="literal">double</code> type.
										</p>
										<p>
											The optional filter expression limits the values considered for computing the standard deviation.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<pre class="synopsis">sum([all|distinct] <span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>filter_expr</em></span>])</pre>
									</td>
									<td>
										<p>
											Totals the (distinct) values in the expression, returning a value of <code class="literal">long, double, float or integer</code> type depending on the expression.
										</p>
										<p>
											The optional filter expression limits the values considered for computing the total.
										</p>
									</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div><br class="table-break">
				<p>
					If your application provides double-type values to an aggregation function, avoid using Not-a-Number (NaN) and infinity. Also when using double-type values, round-off errors (or rounding errors) may occur due to double-type precision. Consider rounding your result value to the desired precision.
				</p>
				<p>
					Each of the aggregation functions above takes an optional filter expression as a parameter. The filter expression must return a boolean-type value and applies to the events considered for the aggregation.
					If a filter expression is provided, then only if the filter expression returns a value of true does the runtime update the aggregation for that event or combination of events.
				</p>
				<p>
					Consider the following example, which computes the quantity fraction of buy orders among all orders:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select sum(quantity, side='buy') / sum(quantity) as buy_fraction from Orders</pre>
				<p>
					Use the <code class="literal">fmin</code> and <code class="literal">fmax</code> aggregation functions instead of the <code class="literal">min</code> and <code class="literal">max</code> aggregation functions when providing a filter expression (the <code class="literal">min</code> and <code class="literal">max</code> functions are also single-row functions).
				</p>
				<p>
					The next example computes the minimum quantity for buy orders and a separate minimum quantity for sell orders:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select fmin(quantity, side='buy'), fmin(quantity, side = 'sell') from Orders</pre>
				<p>
					This sample statement demonstrates specifying grouping criteria for an aggregation function using the <code class="literal">group_by</code> named parameter.
					It computes, for the last one minute of orders, the ratio of orders per account compared to all orders:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select count(*)/count(*, group_by:()) as ratio from Orders#time(1 min) group by account</pre>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-function-aggregation-event"></a>10.2.2. Event Aggregation Functions</h3>
						</div>
					</div>
				</div>
				<p>
					The event aggregation functions return one or more events or event properties. When used with <code class="literal">group by</code> the event aggregation functions
					return one or more events or event properties per group.
				</p>
				<p>
					The <code class="literal">sorted</code> and the <code class="literal">window</code> event aggregation functions require that a data window or named window is declared for the applicable stream.
					They cannot be used on unbound streams.
				</p>
				<p>
					The below table summarizes the event aggregation functions available:
				</p>
				<div class="table"><a id="event_aggregation_func_table"></a>
					<p class="title"><b>Table 10.6. Event Aggregation Functions</b></p>
					<div class="table-contents">
						<table summary="Event Aggregation Functions" border="1">
							<colgroup>
								<col>
								<col>
							</colgroup>
							<thead>
								<tr>
									<th>Function</th>
									<th>Result</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td>first(...)</td>
									<td>
										<p>
											Returns the first event or an event property value of the first event.
										</p>
										<p>
											<a class="xref" href="functionreference.html#epl-function-aggregation-event-first" title="10.2.2.1. First Aggregation Function">Section 10.2.2.1, “First Aggregation Function”</a>.
										</p>
									</td>
								</tr>
								<tr>
									<td>last(...)</td>
									<td>
										<p>
											Returns the last event or an event property value of the last event.
										</p>
										<p>
											<a class="xref" href="functionreference.html#epl-function-aggregation-event-last" title="10.2.2.2. Last Aggregation Function">Section 10.2.2.2, “Last Aggregation Function”</a>.
										</p>
									</td>
								</tr>
								<tr>
									<td>maxby(criteria)</td>
									<td>
										<p>
											Returns the event with the highest sorted value according to criteria expressions.
										</p>
										<p>
											<a class="xref" href="functionreference.html#epl-function-aggregation-event-maxby" title="10.2.2.3. Maxby Aggregation Function">Section 10.2.2.3, “Maxby Aggregation Function”</a>.
										</p>
									</td>
								</tr>
								<tr>
									<td>maxbyever(criteria)</td>
									<td>
										<p>
											Returns the event with the highest sorted value, ever, according to criteria expressions.
										</p>
										<p>
											<a class="xref" href="functionreference.html#epl-function-aggregation-event-maxbyever" title="10.2.2.4. Maxbyever Aggregation Function">Section 10.2.2.4, “Maxbyever Aggregation Function”</a>.
										</p>
									</td>
								</tr>
								<tr>
									<td>minby(criteria)</td>
									<td>
										<p>
											Returns the event with the lowest sorted value according to criteria expressions.
										</p>
										<p>
											<a class="xref" href="functionreference.html#epl-function-aggregation-event-minby" title="10.2.2.5. Minby Aggregation Function">Section 10.2.2.5, “Minby Aggregation Function”</a>.
										</p>
									</td>
								</tr>
								<tr>
									<td>minbyever(criteria)</td>
									<td>
										<p>
											Returns the event with the lowest sorted value, ever, according to criteria expressions.
										</p>
										<p>
											<a class="xref" href="functionreference.html#epl-function-aggregation-event-minbyever" title="10.2.2.6. Minbyever Aggregation Function">Section 10.2.2.6, “Minbyever Aggregation Function”</a>.
										</p>
									</td>
								</tr>
								<tr>
									<td>sorted(criteria)</td>
									<td>
										<p>
											Returns events sorted according to criteria expressions.
										</p>
										<p>
											<a class="xref" href="functionreference.html#epl-function-aggregation-event-sorted" title="10.2.2.7. Sorted Aggregation Function">Section 10.2.2.7, “Sorted Aggregation Function”</a>.
										</p>
										<p>
											Also see <a class="xref" href="aggmethodreference.html#aggmethod-sorted" title="13.3. Aggregation Methods for Sorted Aggregations">Section 13.3, “Aggregation Methods for Sorted Aggregations”</a> for methods such as <code class="literal">lowerKey</code> or <code class="literal">higherKey</code> that operate on <code class="literal">sorted</code> aggregation state.
										</p>
									</td>
								</tr>
								<tr>
									<td>window(...)</td>
									<td>
										<p>
											Returns all events or all event's property values.
										</p>
										<p>
											<a class="xref" href="functionreference.html#epl-function-aggregation-event-window" title="10.2.2.8. Window Aggregation Function">Section 10.2.2.8, “Window Aggregation Function”</a>.
										</p>
										<p>
											Also see <a class="xref" href="aggmethodreference.html#aggmethod-window" title="13.4. Aggregation Methods for Window Aggregations">Section 13.4, “Aggregation Methods for Window Aggregations”</a> for methods such as <code class="literal">first</code> or <code class="literal">countEvents</code> that operate on <code class="literal">window</code> aggregation state.
										</p>
									</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div><br class="table-break">
				<p>
					In connection with named windows and tables, event aggregation functions can also be used in <code class="literal">on-select</code>,
					selects with named window or table in the <code class="literal">from</code> clause, subqueries against named windows or tables and fire-and-forget queries.
				</p>
				<p>
					The event aggregation functions are often useful in connection with enumeration methods and they can provide input events for enumeration. Please see <a class="xref" href="enumerationreference.html" title="Chapter 11. EPL Reference: Enumeration Methods">Chapter 11, <i>EPL Reference: Enumeration Methods</i></a> for more information.
				</p>
				<p>
					When comparing the <code class="literal">last</code> aggregation function to the <code class="literal">prev</code> function, the differences are as follows. The <code class="literal">prev</code> function is not an aggregation function and thereby not sensitive to the presence of <code class="literal">group by</code>. The <code class="literal">prev</code> function accesses data window contents directly and respects the sort order of the data window. The <code class="literal">last</code> aggregation function returns results based on arrival order and tracks data window contents in a separate shared data structure.
				</p>
				<p>
					When comparing the <code class="literal">first</code> aggregation function to the <code class="literal">prevtail</code> function, the differences are as follows. The <code class="literal">prevtail</code> function is not an aggregation function and thereby not sensitive to the presence of <code class="literal">group by</code>. The <code class="literal">prevtail</code> function accesses data window contents directly and respects the sort order of the data window. The <code class="literal">first</code> aggregation function returns results based on arrival order and tracks data window contents in a separate shared data structure.
				</p>
				<p>
					When comparing the <code class="literal">window</code> aggregation function to the <code class="literal">prevwindow</code> function, the differences are as follows. The <code class="literal">prevwindow</code> function is not an aggregation function and thereby not sensitive to the presence of <code class="literal">group by</code>. The <code class="literal">prevwindow</code> function accesses data window contents directly and respects the sort order of the data window. The <code class="literal">window</code> aggregation function returns results based on arrival order and tracks data window contents in a separate shared data structure.
				</p>
				<p>
					When comparing the <code class="literal">count</code> aggregation function to the <code class="literal">prevcount</code> function, the differences are as follows. The <code class="literal">prevcount</code> function is not an aggregation function and thereby not sensitive to the presence of <code class="literal">group by</code>.
				</p>
				<p>
					When comparing the <code class="literal">last</code> aggregation function to the <code class="literal">nth</code> aggregation function, the differences are as follows. The <code class="literal">nth</code> aggregation function does not consider out-of-order deletes (for example with on-delete and sorted windows) and does not revert to the prior expression value when the last event or nth-event was deleted from a data window. The <code class="literal">last</code> aggregation function tracks the data window and reflects out-of-order deletes.
				</p>
				<p>
					From an implementation perspective, the <code class="literal">first</code>, <code class="literal">last</code> and <code class="literal">window</code> aggregation functions share a common data structure for each stream.
					The <code class="literal">sorted</code>, <code class="literal">minby</code> and <code class="literal">maxby</code> aggregation functions share a common data structure for each stream.
				</p>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-function-aggregation-event-first"></a>10.2.2.1. <code class="literal">First</code> Aggregation Function</h4>
							</div>
						</div>
					</div>
					<p>
						The synopsis for the <code class="literal">first</code> aggregation function is:
					</p>
					<pre class="synopsis"><code class="literal">first(</code>*|<span class="emphasis"><em>stream</em></span>.*|<span class="emphasis"><em>value_expression</em></span> [, <span class="emphasis"><em>index_expression</em></span>] [, filter:<span class="emphasis"><em>filter_expression</em></span>])</pre>
					<p>
						The <code class="literal">first</code> aggregation function returns properties of the very first event. When used with <code class="literal">group by</code>, it returns properties of the first event for each group. When specifying an index expression, the function returns properties of the Nth-subsequent event to the first event, all according to order of arrival.
					</p>
					<p>
						The first parameter to the function is required and defines the event properties or expression result to return. The second parameter is an optional <span class="emphasis"><em>index_expression</em></span> that must return an integer value used as an index to evaluate the Nth-subsequent event to the first event.
					</p>
					<p>
						You may specify the wildcard (<code class="literal">*</code>) character in which case the function returns the underlying event of the single selected stream. When selecting a single stream you may specify no parameter instead of wildcard. For joins and subqueries you must use the stream wildcard syntax below.
					</p>
					<p>
						You may specify the stream name and wildcard (<code class="literal">*</code>) character in the <span class="emphasis"><em>stream</em></span><code class="literal">.*</code> syntax. This returns the underlying event for the specified stream.
					</p>
					<p>
						You may specify a <span class="emphasis"><em>value_expression</em></span> to evaluate for the first event. The value expression may not select properties from multiple streams.
					</p>
					<p>
						The <span class="emphasis"><em>index_expression</em></span> is optional. If no index expression is provided, the function returns the first event. If present, the function evaluates the index expression to determine the value for N, and evaluates the Nth-subsequent event to the first event. A value of zero returns the first event and a value of 1 returns
						the event subsequent to the first event. You may not specify event properties in the index expression.
					</p>
					<p>
						The function returns <code class="literal">null</code> if there are no events or when the index is larger than the number of events held. When used with <code class="literal">group by</code>, it returns <code class="literal">null</code> if there are no events for that group or when the index is larger than the number of events held for that group.
					</p>
					<p>
						To explain, consider the statement below which selects the underlying event of the first sensor event held by the length window of 2 events.
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select first(*) from SensorEvent#length(2) </pre>
					<p>
						Assume event E1, event E2 and event E3 are of type SensorEvent. When event E1 arrives the statement outputs the underlying event E1. When event E2 arrives the statement again outputs the underlying event E1. When event E3 arrives the statement outputs the underlying event E2, since
						event E1 has left the data window.
					</p>
					<p>
						The stream wildcard syntax is useful for joins and subqueries. This example demonstrates a subquery that returns the first SensorEvent when a DoorEvent arrives:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select (select first(se.*) from SensorEvent#length(2) as se) from DoorEvent </pre>
					<p>
						The following example shows the use of an index expression. The output value for <code class="literal">f1</code> is the temperature property value of the first event, the value for <code class="literal">f2</code> is the temperature property value of the second event:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select first(temperature, 0) as f1, first(temperature, 1) as f2
from SensorEvent#time(10 sec)</pre>
					<p>
						You may use dot-syntax to invoke a method on the first event. You may also append a property name using dot-syntax.
					</p>
				</div>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-function-aggregation-event-last"></a>10.2.2.2. <code class="literal">Last</code> Aggregation Function</h4>
							</div>
						</div>
					</div>
					<p>
						The synopsis for the <code class="literal">last</code> aggregation function is:
					</p>
					<pre class="synopsis"><code class="literal">last(</code>*|<span class="emphasis"><em>stream</em></span>.*|<span class="emphasis"><em>value_expression</em></span> [, <span class="emphasis"><em>index_expression</em></span>][, filter:<span class="emphasis"><em>filter_expression</em></span>])</pre>
					<p>
						The <code class="literal">last</code> aggregation function returns properties of the very last event. When used with <code class="literal">group by</code>, it returns properties of the last event for each group. When specifying an index expression, the function returns properties of the Nth-prior event to the last event, all according to order of arrival.
					</p>
					<p>
						Similar to the <code class="literal">first</code> aggregation function described above, you may specify the wildcard (<code class="literal">*</code>) character, no parameter or stream name and wildcard (<code class="literal">*</code>) character or a <span class="emphasis"><em>value_expression</em></span> to evaluate for the last event.
					</p>
					<p>
						The <span class="emphasis"><em>index_expression</em></span> is optional. If no index expression is provided, the function returns the last event. If present, the function evaluates the index expression to determine the value for N, and evaluates the Nth-prior event to the last event. A value of zero returns the last event and a value of 1 returns
						the event prior to the last event.
						You may not specify event properties in the index expression.
					</p>
					<p>
						The function returns <code class="literal">null</code> if there are no events or when the index is larger than the number of events held. When used with <code class="literal">group by</code>, it returns <code class="literal">null</code> if there are no events for that group or when the index is larger than the number of events held for that group.
					</p>
					<p>
						The next statement selects the underlying event of the first and last sensor event held by the time window of 10 seconds:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select first(*), last(*) from SensorEvent#time(10 sec) </pre>
					<p>
						The statement shown next selects the last temperature (<code class="literal">f1</code>) and the prior-to-last temperature (<code class="literal">f1</code>) of sensor events in the last 10 seconds:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select last(temperature, 0) as f1, select last(temperature, 1) as f2
from SensorEvent#time(10 sec)</pre>
				</div>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-function-aggregation-event-maxby"></a>10.2.2.3. <code class="literal">Maxby</code> Aggregation Function</h4>
							</div>
						</div>
					</div><a id="d0e30166" class="indexterm"></a>
					<p>
						The synopsis for the <code class="literal">maxby</code> aggregation function is:
					</p>
					<pre class="synopsis"><code class="literal">maxby</code>(<span class="emphasis"><em>sort_criteria_expression</em></span> [asc/desc][, <span class="emphasis"><em>sort_criteria_expression</em></span> [asc/desc]...][, filter:<span class="emphasis"><em>filter_expression</em></span>])</pre>
					<p>
						The <code class="literal">maxby</code> aggregation function returns the greatest of all events, compared by using criteria expressions.
						When used with <code class="literal">group by</code>, it returns the greatest of all events per group.
					</p>
					<p>
						This example statement returns the sensor id and the temperature of the sensor event that had the highest temperature among all sensor events:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select maxby(temperature).sensorId, maxby(temperature).temperature from SensorEvent</pre>
					<p>
						The next EPL returns the sensor event that had the highest temperature and the sensor event that had the lowest temperature, per zone, among the last 10 seconds of sensor events:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select maxby(temperature), minby(temperature) from SensorEvent#time(10 sec) group by zone</pre>
					<p>
						Your EPL may specify multiple criteria expressions. If the sort criteria expression is descending please append the <code class="literal">desc</code> keyword.
					</p>
					<p>
						The following EPL returns the sensor event with the highest temperature and if there are multiple sensor events with the highest temperature the statement
						returns the sensor event that has the newest timestamp value:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select maxby(temperature asc, timestamp desc) from SensorEvent</pre>
					<p>
						Event properties that are listed in criteria expressions must refer to the same event stream and cannot originate from different event streams.
					</p>
					<p>
						If your statement does not define a data window and does not refer to a named window, the semantics of <code class="literal">maxby</code> are the same as <code class="literal">maxbyever</code>.
					</p>
				</div>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-function-aggregation-event-maxbyever"></a>10.2.2.4. <code class="literal">Maxbyever</code> Aggregation Function</h4>
							</div>
						</div>
					</div>
					<p>
						The synopsis for the <code class="literal">maxbyever</code> aggregation function is:
					</p>
					<pre class="synopsis"><code class="literal">maxbyever</code>(<span class="emphasis"><em>sort_criteria_expression</em></span> [asc/desc][, <span class="emphasis"><em>sort_criteria_expression</em></span> [asc/desc]...][, filter:<span class="emphasis"><em>filter_expression</em></span>])</pre>
					<p>
						The <code class="literal">maxbyever</code> aggregation function returns the greatest of all events that ever occurred, compared by using criteria expressions.
						When used with <code class="literal">group by</code>, it returns the greatest of all events that ever occurred per group.
					</p>
					<p>
						Compared to the <code class="literal">maxby</code> aggregation function the <code class="literal">maxbyever</code> does not consider the data window or named window contents
						and instead considers all arriving events.
					</p>
					<p>
						The next EPL computes the difference, per zone, between the maximum temperature considering all events and the maximum temperature considering
						only the events in the last 10 seconds:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select maxby(temperature).temperature - maxbyever(temperature).temperature 
from SensorEvent#time(10) group by zone</pre>
				</div>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-function-aggregation-event-minby"></a>10.2.2.5. <code class="literal">Minby</code> Aggregation Function</h4>
							</div>
						</div>
					</div><a id="d0e30270" class="indexterm"></a>
					<p>
						The synopsis for the <code class="literal">minby</code> aggregation function is:
					</p>
					<pre class="synopsis"><code class="literal">minby</code>(<span class="emphasis"><em>sort_criteria_expression</em></span> [asc/desc][, <span class="emphasis"><em>sort_criteria_expression</em></span> [asc/desc]...][, filter:<span class="emphasis"><em>filter_expression</em></span>])</pre>
					<p>
						Similar to the <code class="literal">maxby</code> aggregation function, the <code class="literal">minby</code> aggregation function returns the lowest of all events, compared by using criteria expressions.
						When used with <code class="literal">group by</code>, it returns the lowest of all events per group.
					</p>
					<p>
						Please review the section on <code class="literal">maxby</code> for more information.
					</p>
				</div>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-function-aggregation-event-minbyever"></a>10.2.2.6. <code class="literal">Minbyever</code> Aggregation Function</h4>
							</div>
						</div>
					</div>
					<p>
						Similar to the <code class="literal">maxbyever</code> aggregation function, the <code class="literal">minbyever</code> aggregation function returns the lowest of all events that ever occurred, compared by using criteria expressions.
						When used with <code class="literal">group by</code>, it returns the lowest of all events per group that ever occured.
					</p>
					<p>
						Please review the section on <code class="literal">maxbyever</code> for more information.
					</p>
				</div>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-function-aggregation-event-sorted"></a>10.2.2.7. <code class="literal">Sorted</code> Aggregation Function</h4>
							</div>
						</div>
					</div><a id="d0e30333" class="indexterm"></a><a id="d0e30336" class="indexterm"></a>
					<p>
						The synopsis for the <code class="literal">sorted</code> aggregation function is:
					</p>
					<pre class="synopsis"><code class="literal">sorted</code>(<span class="emphasis"><em>sort_criteria_expression</em></span> [asc/desc][, <span class="emphasis"><em>sort_criteria_expression</em></span> [asc/desc]...][, filter:<span class="emphasis"><em>filter_expression</em></span>])</pre>
					<p>
						The <code class="literal">sorted</code> aggregation function maintains a list of events sorted according to criteria expressions.
						When used with <code class="literal">group by</code>, it maintains a list of events sorted according to criteria expressions per group.
					</p>
					<p>
						The <code class="literal">sorted</code> aggregation offers a number of additional lookup methods such as <code class="literal">lowerKey</code> or <code class="literal">higherKey</code>, see <a class="xref" href="aggmethodreference.html#aggmethod-sorted" title="13.3. Aggregation Methods for Sorted Aggregations">Section 13.3, “Aggregation Methods for Sorted Aggregations”</a>.
					</p>
					<p>
						The sample EPL listed next returns events sorted according to temperature ascending for the same zone:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select sorted(temperature) from SensorEvent group by zone</pre>
					<p>
						Your EPL may specify multiple criteria expressions. If the sort criteria expression is descending please append the <code class="literal">desc</code> keyword.
					</p>
					<p>
						Enumeration methods can be useful in connection with <code class="literal">sorted</code> as the function provides the sorted events as input.
					</p>
					<p>
						This statement finds the sensor event that when sorted according to temperature is the first sensor event for a Friday timestamp among sensor events for the same zone:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select sorted(temperature).firstOf(v =&gt; timestamp.getDayOfWeek()=6) from SensorEvent</pre>
					<p>
						Event properties that are listed in criteria expressions must refer to the same event stream and cannot originate from different event streams.
					</p>
					<p>
						If used in a regular select statement, the use of <code class="literal">sorted</code> requires that your EPL defines a data window for the stream or utilizes a named window.
					</p>
				</div>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-function-aggregation-event-window"></a>10.2.2.8. <code class="literal">Window</code> Aggregation Function</h4>
							</div>
						</div>
					</div>
					<p>
						The synopsis for the <code class="literal">window</code> aggregation function is:
					</p>
					<pre class="synopsis"><code class="literal">window(</code>*|<span class="emphasis"><em>stream</em></span>.*|<span class="emphasis"><em>value_expression</em></span> [, filter:<span class="emphasis"><em>filter_expression</em></span>])</pre>
					<p>
						The <code class="literal">window</code> aggregation function returns all rows. When used with <code class="literal">group by</code>, it returns the rows for each group.
					</p>
					<p>
						The <code class="literal">window</code> aggregation offers a number of additional lookup methods such as <code class="literal">last</code> or <code class="literal">listReference</code>, see <a class="xref" href="aggmethodreference.html#aggmethod-window" title="13.4. Aggregation Methods for Window Aggregations">Section 13.4, “Aggregation Methods for Window Aggregations”</a>.
					</p>
					<p>
						Similar to the <code class="literal">first</code> aggregation function described above, you may specify the wildcard (<code class="literal">*</code>) character or stream name and wildcard (<code class="literal">*</code>) character or a <span class="emphasis"><em>value_expression</em></span> to evaluate for all events.
					</p>
					<p>
						The function returns <code class="literal">null</code> if there are no rows. When used with <code class="literal">group by</code>, it returns <code class="literal">null</code> if there are no rows for that group.
					</p>
					<p>
						The next statement selects the underlying event of all events held by the time window of 10 seconds:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select window(*) from SensorEvent#time(10 sec) </pre>
					<p>
						If used in a regular select statement, the <code class="literal">window</code> aggregation function requires that your stream is bound by a data window or a named window. You may not use the <code class="literal">window</code> aggregation function on unbound streams with the exception of fire-and-forget queries or subqueries.
					</p>
					<p>
						This example statement assumes that the <code class="literal">OrderWindow</code> named window exists. For each event entering or leaving the <code class="literal">OrderWindow</code> named window
						it outputs the total amount removing negative amounts:
					</p>
					<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select window(*).where(v =&gt; v.amount &gt; 0).aggregate(0d, (r, v) =&gt; r + v.amount) from OrderWindow </pre>
				</div>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-function-aggregation-approx"></a>10.2.3. Approximation Aggregation Functions</h3>
						</div>
					</div>
				</div>
				<p>
					Approximation aggregation functions are aggregations that perform approximate analysis. Compared to the previously-introduced aggregation functions,
					the functions discussed here have a degree of accuracy and probabilistic behavior.
				</p>
				<div class="sect3" lang="en-US">
					<div class="titlepage">
						<div>
							<div>
								<h4 class="title"><a id="epl-function-aggregation-approx-countminsketch"></a>10.2.3.1. Count-Min Sketch</h4>
							</div>
						</div>
					</div>
					<p>
						Count-min sketch (or CM sketch) is a probabilistic sub-linear space streaming algorithm (source: Wikipedia).
						Count-min sketch computes an approximate frequency, without retaining distinct values in memory, making the algorithm suitable for summarizing very large spaces of distinct values.
						The estimated count can be used for estimated top-K and estimated heavy-hitters, for example.
					</p>
					<p>
						The original and detail of the algorithm is described in the paper by Graham Cormode and S. Muthukrishnan. An improved data stream summary:
						The Count-min sketch and its applications (2004. 10.1016/j.jalgor.2003.12.001).
					</p>
					<p>
						Count-min sketch can only be used with tables and is not available as an aggregation function other than in a table declaration.
					</p>
					<p>
						Count-min sketch does not consider events leaving a data window and does not process a remove stream.
					</p>
					<div class="sect4" lang="en-US">
						<div class="titlepage">
							<div>
								<div>
									<h5 class="title"><a id="epl-function-aggregation-approx-countminsketch-declare"></a>10.2.3.1.1. Declaration</h5>
								</div>
							</div>
						</div>
						<p>
							The table column type for Count-min sketch is <code class="literal">countMinSketch</code>.
						</p>
						<p>
							For example, the next EPL declares a table that holds a Count-min sketch (does not provision a top-K):
						</p>
						<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">create table WordCountTable(wordcms countMinSketch())</pre>
						<p>
							You can parameterize the algorithm by providing a JSON-format structure to the declaration.
							The available parameters are all optional:
						</p>
						<div class="table"><a id="countminsketch_params"></a>
							<p class="title"><b>Table 10.7. Count-min Sketch Parameters</b></p>
							<div class="table-contents">
								<table summary="Count-min Sketch Parameters" border="1">
									<colgroup>
										<col>
										<col>
									</colgroup>
									<thead>
										<tr>
											<th>Name</th>
											<th>Description</th>
										</tr>
									</thead>
									<tbody>
										<tr>
											<td><code class="literal">epsOfTotalCount</code></td>
											<td>
												<p>
													Specifies the accuracy (number of values counted * accuracy &gt;= number of errors) of type double.
												</p>
												<p>
													This value defaults to 0.0001.
												</p>
											</td>
										</tr>
										<tr>
											<td><code class="literal">confidence</code></td>
											<td>
												<p>
													Provides the certainty with which you reach the accuracy of type double.
												</p>
												<p>
													The default is 0.99.
												</p>
											</td>
										</tr>
										<tr>
											<td><code class="literal">seed</code></td>
											<td>
												<p>
													A seed value for computing hash codes of type integer.
												</p>
												<p>
													This default is 123456.
												</p>
											</td>
										</tr>
										<tr>
											<td><code class="literal">topk</code></td>
											<td>
												<p>
													The number of top-K values as an integer. If null, the algorithm maintains no top-K list.
												</p>
												<p>
													This value defaults to null (no top-K available).
												</p>
											</td>
										</tr>
										<tr>
											<td><code class="literal">agent</code></td>
											<td>
												<p>
													The agent is an extension API class that can interact with Count-min sketch state and also receives the value objects.
													The agent defines the type of the values that can be counted. The default agent only allows string-type values
													and utilizes UTF-16 charset encoding.
												</p>
												<p>
													The default agent is <code class="literal">com.espertech.esper.common.client.util.CountMinSketchAgentStringUTF16</code>.
												</p>
											</td>
										</tr>
									</tbody>
								</table>
							</div>
						</div><br class="table-break">
						<p>
							The next example EPL declares all available parameters:
						</p>
						<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">create table WordCountTable (wordcms countMinSketch({
  epsOfTotalCount: 0.000002,
  confidence: 0.999,
  seed: 38576,
  topk: 20,
  agent: 'com.mycompany.CountMinSketchCustomAgent'
}))</pre>
						<p>
							The default for the <code class="literal">topk</code> parameter is <code class="literal">null</code>. Thereby the runtime by default does not compute top-K.
							By specifying a positive integer value for <code class="literal">topk</code> the algorithm maintains a list of values representing the top estimated counts.
						</p>
						<p>
							By default, the Count-min sketch group of aggregation functions operates on string-type values only.
							The aggregation function allows registering an agent that can handle any other type of value objects and that allows overriding behavior.
							The agent class must implement the interface <code class="literal">com.espertech.esper.common.client.util.CountMinSketchAgent</code>.
							Please see the JavaDoc for implementing an agent. The agent API is an extension API and is subject to change between versions.
						</p>
					</div>
					<div class="sect4" lang="en-US">
						<div class="titlepage">
							<div>
								<div>
									<h5 class="title"><a id="epl-function-aggregation-approx-countminsketch-add"></a>10.2.3.1.2. Counting Values</h5>
								</div>
							</div>
						</div>
						<p>
							The <code class="literal">countMinSketchAdd</code> function counts value(s). It expects a single parameter expression returning the value(s) to be counted.
							The function can only be used with statements that utilize <code class="literal">into table</code> and can accept a <code class="literal">filter:</code> filter expression as a parameter.
						</p>
						<p>
							This example EPL counts words:
						</p>
						<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">into table WordCountTable select countMinSketchAdd(word) as wordcms from WordEvent</pre>
					</div>
					<div class="sect4" lang="en-US">
						<div class="titlepage">
							<div>
								<div>
									<h5 class="title"><a id="epl-function-aggregation-approx-countminsketch-freq"></a>10.2.3.1.3. Estimating Current Count</h5>
								</div>
							</div>
						</div>
						<p>
							The <code class="literal">countMinSketchFrequency</code> function returns an estimated count for a given value.
							It expects a single parameter expression returning the value(s) for which to estimate and return the long-type count.
							The function can only be used as a table-access function against a table column that declares the aggregation <code class="literal">countMinSketch</code>.
						</p>
						<p>
							The next example EPL returns, when a <code class="literal">EstimateWordCountEvent</code> event arrives, the estimated frequency of a given word:
						</p>
						<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select WordCountTable.wordcms.countMinSketchFrequency(word) from EstimateWordCountEvent</pre>
					</div>
					<div class="sect4" lang="en-US">
						<div class="titlepage">
							<div>
								<div>
									<h5 class="title"><a id="epl-function-aggregation-approx-countminsketch-topk"></a>10.2.3.1.4. Obtaining Top-K</h5>
								</div>
							</div>
						</div>
						<p>
							The <code class="literal">countMinSketchTopK</code> function returns top-K. The function expects no parameters.
							The function can only be used as a table-access function against a table column that declares the aggregation <code class="literal">countMinSketch</code>
							and only if the Count-min sketch was parameterized with a non-null <code class="literal">topk</code> parameter (the default is <code class="literal">null</code>, see declaration above).
						</p>
						<p>
							The function returns an array of <code class="literal">com.espertech.esper.common.client.util.CountMinSketchTopK</code>.
						</p>
						<p>
							The following EPL outputs top-K every 10 seconds:
						</p>
						<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select WordCountTable.wordcms.countMinSketchTopk() from pattern[every timer:interval(10 sec)]</pre>
					</div>
					<div class="sect4" lang="en-US">
						<div class="titlepage">
							<div>
								<div>
									<h5 class="title"><a id="epl-function-aggregation-approx-agent"></a>10.2.3.1.5. Agent API Example</h5>
								</div>
							</div>
						</div>
						<p>
							We provide a sample agent code that handles String-type values below. The complete code is available for class <code class="literal">CountMinSketchAgentStringUTF16</code> as part of sources.
						</p>
						<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">public class CountMinSketchAgentStringUTF16 implements CountMinSketchAgent {
  public Class[] getAcceptableValueTypes() {
    return new Class[] {String.class};
  }

  public void add(CountMinSketchAgentContextAdd ctx) {
    String text = (String) ctx.getValue();
    if (text == null) {
      return;
    }
    byte[] bytes = toBytesUTF16(text);
    ctx.getState().add(bytes, 1);  // increase count by 1
  }

  public Long estimate(CountMinSketchAgentContextEstimate ctx) {
    String text = (String) ctx.getValue();
    if (text == null) {
      return null;
    }
    byte[] bytes = toBytesUTF16(text);
    return ctx.getState().frequency(bytes);
  }
}</pre>
					</div>
				</div>
			</div>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="epl-function-aggregation-add"></a>10.2.4. Additional Aggregation Functions</h3>
						</div>
					</div>
				</div>
				<p>
					EPL provides the following additional aggregation functions beyond those in the SQL standard:
				</p>
				<div class="table"><a id="epl-grouping-aggregate-func-table-ext"></a>
					<p class="title"><b>Table 10.8. Syntax and Results of EPL Aggregation Functions</b></p>
					<div class="table-contents">
						<table summary="Syntax and Results of EPL Aggregation Functions" border="1">
							<colgroup>
								<col>
								<col>
							</colgroup>
							<thead>
								<tr>
									<th>Aggregate Function</th>
									<th>Result</th>
								</tr>
							</thead>
							<tbody>
								<tr>
									<td>
										<p>
											countever(<span class="emphasis"><em>*</em></span> [, <span class="emphasis"><em>filter_expr</em></span>])
										</p>
										<p>
											countever(<span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>filter_expr</em></span>])
										</p>
									</td>
									<td>
										<p>
											The <code class="literal">countever</code> aggregation function returns the number of events ever.
											When used with <code class="literal">group by</code> it returns the number of events ever for that group.
										</p>
										<p>
											When used with a data window, the result of the function does not change as data points leave a data window. Use the <code class="literal">count(*)</code> or <code class="literal">prevcount</code> function to return counts relative to a data window.
										</p>
										<p>
											The optional filter expression limits the values considered for counting rows. The <code class="literal">distinct</code> keyword is not allowed. When an expression is provided
											instead of wildcard, counts the non-null values.
										</p>
										<p>
											The next example statement outputs the count-ever for sell orders:
										</p>
										<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select countever(*, side='sell') from Order</pre>
									</td>
								</tr>
								<tr>
									<td>firstever(<span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>filter_expr</em></span>])</td>
									<td>
										<p>
											The <code class="literal">firstever</code> aggregation function returns the very first value ever. When used with <code class="literal">group by</code> it returns the first value ever for that group.
										</p>
										<p>
											When used with a data window, the result of the function does not change as data points leave a data window. Use the <code class="literal">first</code> or <code class="literal">prevtail</code> function to return values relative to a data window.
										</p>
										<p>
											The optional filter expression limits the values considered for retaining the first-ever value.
										</p>
										<p>
											The next example statement outputs the first price ever for sell orders:
										</p>
										<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select firstever(price, side='sell') from Order</pre>
									</td>
								</tr>
								<tr>
									<td>lastever(<span class="emphasis"><em>expression</em></span> [, <span class="emphasis"><em>filter_expr</em></span>])</td>
									<td>
										<p>
											Returns the last value or last value per group, when used with <code class="literal">group by</code>.
										</p>
										<p>
											This sample statement outputs the total price, the first price and the last price per symbol for the last 30 seconds of events and every 5 seconds:
										</p>
										<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select symbol, sum(price), lastever(price), firstever(price)
from StockTickEvent#time(30 sec) 
group by symbol
output every 5 sec</pre>
										<p>
											When used with a data window, the result of the function does not change as data points leave a data window (for example when all data points leave the data window). Use the <code class="literal">last</code> or <code class="literal">prev</code> function to return values relative to a data window.
										</p>
										<p>
											The optional filter expression limits the values considered for retaining the last-ever value.
										</p>
										<p>
											The next example statement outputs the last price (ever) for sell orders:
										</p>
										<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select lastever(price, side='sell') from Order</pre>
									</td>
								</tr>
								<tr>
									<td>leaving([filter:<span class="emphasis"><em>filter_expression</em></span>])</td>
									<td>
										<p>
											Returns true when any remove stream data has passed, for use in the <code class="literal">having</code> clause to output only when a data window has filled.
										</p>
										<p>
											The <code class="literal">leaving</code> aggregation function is useful when you want to trigger output after a data window has a remove stream data point. Use the <code class="literal">output after</code> syntax as an alternative to output after a time interval.
										</p>
										<p>
											This sample statement uses <code class="literal">leaving()</code> to output after the first data point leaves the data window, ignoring the first datapoint:
										</p>
										<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select symbol, sum(price) 
from StockTickEvent#time(30 sec) 
having leaving()</pre>
									</td>
								</tr>
								<tr>
									<td>nth(<span class="emphasis"><em>expression</em></span>, <span class="emphasis"><em>N_index</em></span> [, filter:<span class="emphasis"><em>filter_expression</em></span>])</td>
									<td>
										<p>
											Returns the Nth oldest element; If N=0 returns the most recent value. If N=1 returns the value before the most recent value. If N is larger than the events held in the data window for this group, returns null.
										</p>
										<p>
											A maximum N historical values are stored, so it can be safely used to compare
											recent values in data windows with a large number of events without incurring excessive overhead.
										</p>
										<p>
											As compared to the <code class="literal">prev</code> row function, this aggregation function works within the current <code class="literal">group by</code> group, see <a class="xref" href="processingmodel.html#processingmodel_aggregation" title="2.15. Basic Aggregated Statement Types">Section 2.15, “Basic Aggregated Statement Types”</a>.
										</p>
										<p>
											This statement outputs every 2 seconds the groups that have new data and their last price and the previous-to-last price:
										</p>
										<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select symbol, nth(price, 1), last(price) 
from StockTickEvent 
group by symbol
output last every 2 sec</pre>
									</td>
								</tr>
								<tr>
									<td>
										<p>
											rate(<span class="emphasis"><em>number_of_seconds</em></span> [, filter:<span class="emphasis"><em>filter_expression</em></span>])
										</p>
									</td>
									<td>
										<p>
											Returns an event arrival rate per second over the provided number of seconds, computed based on runtime time.
										</p>
										<p>
											Returns null until events fill the number of seconds. Useful with <code class="literal">output snapshot</code> to output a current rate. This function footprint is for use without a data window onto the stream(s).
										</p>
										<p>
											A sample statement to output, every 2 seconds, the arrival rate per second considering the last 10 seconds of events is shown here:
										</p>
										<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select rate(10) from StockTickEvent
output snapshot every 2 sec</pre>
										<p>
											The aggregation function retains a runtime timestamp value for each arriving event.
										</p>
									</td>
								</tr>
								<tr>
									<td>
										<p>
											rate(<span class="emphasis"><em>timestamp_property</em></span>[, <span class="emphasis"><em>accumulator</em></span>] [, filter:<span class="emphasis"><em>filter_expression</em></span>])
										</p>
									</td>
									<td>
										<p>
											Returns an event arrival rate over the data window including the last remove stream event. The <span class="emphasis"><em>timestamp_property</em></span>
											is the name of a long-type property of the event that provides a timestamp value.
										</p>
										<p>
											The first parameter is a property name or expression providing millisecond timestamp values.
										</p>
										<p>
											The optional second parameter is a property or expression for computing an accumulation rate: If a value is provided as a second parameter then the accumulation rate for that quantity is returned (e.g. turnover in dollars per second).
										</p>
										<p>
											This footprint is designed for use with a data window and requires a data window declared onto the stream. Returns null until events start leaving the window.
										</p>
										<p>
											This sample statement outputs event rate for each group (symbol) with fixed sample size of four events
											(and considering the last event that left). The <code class="literal">timestamp</code> event property must be part of the event for this to work.
										</p>
										<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select colour, rate(timestamp) as rate 
from StockTickEvent#groupwin(symbol)#length(4) 
group by symbol</pre>
									</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div><br class="table-break">
				<p>
					Built-in aggregation functions can be disabled via configuration (see <a class="xref" href="configuration.html#configuration-compiler-expression-extendedagg" title="17.5.6.3. Extended Built-in Aggregation Functions">Section 17.5.6.3, “Extended Built-in Aggregation Functions”</a>). A custom aggregation function of the same name as a built-on function may be registered to override the built-in function.
				</p>
			</div>
		</div>
		<div class="sect1" lang="en-US">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title"><a id="epl-function-user-defined"></a>10.3. User-Defined Functions</h2>
					</div>
				</div>
			</div><a id="d0e30991" class="indexterm"></a><a id="d0e30994" class="indexterm"></a><a id="d0e30999" class="indexterm"></a>
			<p>
				A user-defined function (UDF) is a single-row function that can be invoked anywhere as an expression itself or within an expresson. The function must simply be a public static method of a public class
				that the classloader can resolve at statement deployment time. The compiler resolves the function reference at compile-time and verifies parameter types.
			</p>
			<p>
				For information on calling external services via instance method invocation, please see <a class="xref" href="epl_clauses.html#variable_eventtype" title="5.17.5. Class and Event-Type Variables">Section 5.17.5, “Class and Event-Type Variables”</a>. For invoking methods on events, please see <a class="xref" href="epl_clauses.html#epl-from-clause-stream-name" title="5.4.5. Using the Stream Name">Section 5.4.5, “Using the Stream Name”</a>
			</p>
			<p>
				You may register your own function name for the user-defined function. Please see the instructions in <a class="xref" href="extension.html#custom-singlerow-function" title="22.2. Single-Row Function">Section 22.2, “Single-Row Function”</a> for registering a function name for a user-defined single-row function.
			</p>
			<p>
				A single-row function that has been registered with a function name can simply be referenced as <span class="emphasis"><em>function_name</em></span><code class="literal">(</code><span class="emphasis"><em>parameters</em></span><code class="literal">)</code> thus statements can be less cluttered as no class name is required.
				The compiler also optimizes evaluation of such registered single-row functions when used in filter predicate expressions as described in <a class="xref" href="extension.html#custom-single-filter-optimizable" title="22.2.4. Single-Row Functions in Filter Predicate Expressions">Section 22.2.4, “Single-Row Functions in Filter Predicate Expressions”</a>.
			</p>
			<p>
				An example statement that utilizes the <code class="literal">discount</code> function is shown next (assuming that function has been registered).
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select discount(quantity, price) from OrderEvent</pre>
			<p>
				When selecting from a single stream, use the wildcard <code class="literal">(*)</code> character to pass the underlying event:
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select discount(*) from OrderEvent</pre>
			<p>
				Alternatively use the stream alias or EPL pattern tag to pass an event:
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select discount(oe) from OrderEvent as oe</pre>
			<p>
				User-defined functions can be also be invoked on instances of an event: Please see <a class="xref" href="epl_clauses.html#epl-from-clause-stream-name" title="5.4.5. Using the Stream Name">Section 5.4.5, “Using the Stream Name”</a> to invoke event instance methods on a named stream.
			</p>
			<p>
				Note that user-defined functions (not single-row functions) are candidate for caching their return result if the parameters passed are constants and they are not used chained. Please see below for details and configuration.
			</p>
			<p>
				The example below assumes a class <code class="literal">MyClass</code> that exposes a public static method <code class="literal">myFunction</code> accepting 2 parameters, and
				returing a numeric type such as <code class="literal">double</code>.
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select 3 * com.mycompany.MyClass.myFunction(price, volume) as myValue 
from StockTick#time(30 sec)</pre>
			<p>
				User-defined functions also take array parameters as this example shows. The section on <a class="xref" href="epl-operator.html#epl-operator-ref-array" title="9.5. Array Definition Operator">Section 9.5, “Array Definition Operator”</a> outlines in more detail the types of arrays produced.
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select * from RFIDEvent where com.mycompany.rfid.MyChecker.isInZone(zone, {10, 20, 30})</pre>
			<p>
				Java class names have to be fully qualified (e.g. java.lang.Math) but EPL provides a mechanism for user-controlled imports of classes and packages as outlined in <a class="xref" href="configuration.html#configuration-common-imports" title="17.4.2. Class and Package Imports">Section 17.4.2, “Class and Package Imports”</a>.
			</p>
			<p>
				User-defined functions can return any value including <code class="literal">null</code>, Java objects or arrays. Therefore user-defined functions can serve to transform, convert or map events, or to extract information and assemble further events.
			</p>
			<p>
				The following statement is a simple pattern that looks for events of type E1 that are followed by events of type E2. It assigns the tags "e1" and "e2" that the function can use to assemble a final event for output:
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select MyLib.mapEvents(e1, e2) from pattern [every e1=E1 -&gt; e2=E2]</pre>
			<p>
				User-defined functions may also be chained: If a user-defined function returns an object then the object can itself be the target of the next function call and so on.
			</p>
			<p>
				Assume that there is a <code class="literal">calculator</code> function in the <code class="literal">MyLib</code> class that returns a class which provides the <code class="literal">search</code> method taking two parameters. The EPL
				that takes the result of the <code class="literal">calculator</code> function and that calls the <code class="literal">search</code> method on the result and returns its return value is shown below:
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select MyLib.calculator().search(zonevariable, zone) from RFIDEvent]</pre>
			<p>
				A user-defined function should be implemented thread-safe.
			</p>
			<div class="sect3" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h4 class="title"><a id="epl-function-user-defined-converting"></a>10.3.1. Event Type Conversion via User-Defined Function</h4>
						</div>
					</div>
				</div>
				<p>
					A function that converts from one event type to another event type is shown in the next example. The first statement declares a stream that consists of MyEvent events. The second statement employs a conversion function to convert MyOtherEvent events to events of type MyEvent:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">insert into MyStream select * from MyEvent
	insert into MyStream select MyLib.convert(other) from MyOtherEvent as other</pre>
				<p>
					In the example above, assuming the event classes MyEvent and MyOtherEvent are Java classes, the static method should have the following footprint:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">public static MyEvent convert(MyOtherEvent otherEvent)</pre>
			</div>
			<div class="sect3" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h4 class="title"><a id="epl-function-user-defined-cached"></a>10.3.2. User-Defined Function Result Cache</h4>
						</div>
					</div>
				</div>
				<p>
					For user-defined functions that take no parameters or only constants as parameters the runtime automatically caches the return result of the function, and invokes the function only once. This is beneficial to performance
					if your function indeed returns the same result for the same input parameters.
				</p>
				<p>
					You may disable caching of return values of user-defined functions via configuration as described in <a class="xref" href="configuration.html#configuration-compiler-expression-udfcache" title="17.5.6.2. User-Defined Function or Static Method Cache">Section 17.5.6.2, “User-Defined Function or Static Method Cache”</a>.
				</p>
			</div>
			<div class="sect3" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h4 class="title"><a id="epl-function-user-defined-matching"></a>10.3.3. Parameter Matching</h4>
						</div>
					</div>
				</div>
				<p>
					EPL follows Java standards in terms of widening, performing widening automatically in cases where widening type conversion is allowed without loss of precision, for both boxed and primitive types.
				</p>
				<p>
					When user-defined functions are overloaded, the function with the best match is selected based on how well the arguments to a function can match up with the parameters, giving preference to the function that requires the least number of widening conversions.
				</p>
				<p>
					User-defined functions that can receive an arbitrary number of parameter values can use <span class="emphasis"><em>varargs</em></span>, i.e. can define a function such as <code class="literal">function(T arg1, T... args) {...}</code>.
				</p>
				<p>
					Boxing and unboxing of arrays is not supported in UDF as it is not supported in Java. For example, an array of <code class="literal">Integer</code> and an array of <code class="literal">int</code> are not compatible types.
				</p>
				<p>
					When passing the event or underlying event to your method, either declare the parameter to take <code class="literal">EventBean</code> (i.e. <code class="literal">myfunc(EventBean event)</code>)
					or as the underlying event type (i.e. <code class="literal">myfunc(OrderEvent event)</code>).
				</p>
				<p>
					When using <code class="literal">{}</code> array syntax in EPL, the resulting type is always a boxed type: <code class="literal">"{1, 2}"</code> is an array of <code class="literal">Integer</code> (and not <code class="literal">int</code> since it may contain null values), <code class="literal">"{1.0, 2d}"</code> is an array of <code class="literal">Double</code> and <code class="literal">"{'A', "B"}"</code> is an array of <code class="literal">String</code>, while <code class="literal">"{1, "B", 2.0}"</code> is an array of <code class="literal">Object</code> (<code class="literal">Object[]</code>).
				</p>
			</div>
			<div class="sect3" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h4 class="title"><a id="epl-function-user-defined-receivectx"></a>10.3.4. Receiving a Context Object</h4>
						</div>
					</div>
				</div>
				<p>
					The runtime can pass an object containing contextual information such as statement name, function name, runtime URI and context partition id to your
					method. The container for this information is <code class="literal">EPLMethodInvocationContext</code> in package <code class="literal">com.espertech.esper.common.client.hook.expr</code>.
					Please declare your method to take <code class="literal">EPLMethodInvocationContext</code> as the last parameter. The runtime then passes the information along.
				</p>
				<p>
					A sample method footprint and EPL are shown below:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">public static double computeSomething(double number, EPLMethodInvocationContext context) {...}</pre>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select MyLib.computeSomething(10) from MyEvent</pre>
			</div>
		</div>
		<div class="sect1" lang="en-US">
			<div class="titlepage">
				<div>
					<div>
						<h2 class="title"><a id="functionreference-transpose"></a>10.4. Select-Clause <code class="literal">Transpose</code> Function</h2>
					</div>
				</div>
			</div>
			<p>
				The <code class="literal">transpose</code> function is only valid in the select-clause and indicates that the result of the parameter expression should become the underlying event object of the output event.
			</p>
			<p>
				The <code class="literal">transpose</code> function takes a single expression as a parameter. The result object of the parameter expression is subject to transposing as described below.
			</p>
			<p>
				The function can be useful with <code class="literal">insert into</code> to allow an object returned by an expression to become the event itself in the output stream.
			</p>
			<p>
				Any expression returning a Java object can be used with the <code class="literal">transpose</code> function. Typical examples for expressions are a static method invocation, the result of an enumeration method, a plug-in single row function or a subquery.
			</p>
			<p>
				The examples herein assume that a single-row function by name <code class="literal">makeEvent</code> returns an <code class="literal">OrderEvent</code> instance (a POJO object, not shown).
			</p>
			<p>
				The following EPL takes the result object of the invocation of the <code class="literal">makeEvent</code> method (assumed to be an OrderEvent instance) and returns the OrderEvent instance as the underlying event of the output event:
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select transpose(makeEvent(oi)) from OrderIndication oi</pre>
			<p>
				Your select-clause can select additional properties or expressions. In this case the output event underlying object is a pair of the expression result object and the additional properties.
			</p>
			<p>
				The next EPL also selects the <code class="literal">origin</code> property of the order indication event. The output event is a pair of the OrderEvent instance and a map containing the property name and value of origin:
			</p>
			<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">select origin, transpose(makeEvent(oi)) from OrderIndication oi</pre>
			<p>
				If the <code class="literal">transpose</code> function is not a top-level function, i.e. if it occurs within another expression or within any other clause then the select-clause, the function simply returns the expression result of the parameter expression.
			</p>
			<div class="sect2" lang="en-US">
				<div class="titlepage">
					<div>
						<div>
							<h3 class="title"><a id="functionreference-transpose-insert-into"></a>10.4.1. Transpose with Insert-Into</h3>
						</div>
					</div>
				</div>
				<p>
					You may insert transposed output events into another stream.
				</p>
				<p>
					If the stream name in the insert-into clause is already associated to an event type, the compiler checks whether the event type associated to the stream name provided in the insert-into clause matches the event type associated to the object returned by the expression. If the stream name in the insert-into clause is not already associated to an existing event type the compiler associates a new event type using the stream name provided in the insert-into clause.
				</p>
				<p>
					The type returned by the expression must match the event representation that is defined for the stream, i.e. must be a subtype or implementation of the respective class (POJO, object-array or Map).
				</p>
				<p>
					For example, the next statement associates the stream name <code class="literal">OrderEvent</code> with a class:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">create schema OrderEvent as com.mycompany.OrderEvent</pre>
				<p>
					A statement can insert into the <code class="literal">OrderEvent</code> stream the <code class="literal">OrderEvent</code> instance returned by the <code class="literal">makeEvent</code> method, as follows:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">insert into OrderEvent select transpose(makeEvent(oi)) from OrderIndication oi</pre>
				<p>
					It is not valid to select additional properties or expressions in this case, as they would not be part of the output event. The following is not valid:
				</p>
				<pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="">// not valid
insert into OrderEvent select origin, transpose(makeEvent(oi)) from OrderIndication oi</pre>
			</div>
		</div>
	</div>
	<ul xmlns:d="http://docbook.org/ns/docbook" class="docnav">
		<li class="previous"><a accesskey="p" href="epl-operator.html"><strong>Prev</strong>Chapter 9. EPL Reference: Operators</a></li>
		<li class="up"><a accesskey="u" href="#"><strong>Top of page</strong></a></li>
		<li class="home"><a accesskey="h" href="index.html"><strong>Front page</strong></a></li>
		<li class="next"><a accesskey="n" href="enumerationreference.html"><strong>Next</strong>Chapter 11. EPL Reference: Enumeration Methods</a></li>
	</ul>
</body>

</html>